js判斷數(shù)據(jù)類型

一、typeof 直接返回數(shù)據(jù)類型字段,但是無法判斷數(shù)組、null、對象

成都創(chuàng)新互聯(lián)"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機版的企業(yè)網(wǎng)站。實現(xiàn)跨屏營銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營銷需求!成都創(chuàng)新互聯(lián)具備承接各種類型的做網(wǎng)站、成都網(wǎng)站建設(shè)項目的能力。經(jīng)過十載的努力的開拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評。

typeof 1
"number"

typeof NaN
"number"

typeof "1"
"string"

typeof true
"boolean"

typeof undefined
"undefined"

typeof null
"object"

typeof []
"object"

typeof {}
"object"
其中 null, [], {}都返回 "object"

二、instanceof 判斷某個實例是不是屬于原型

// 構(gòu)造函數(shù)
function Fruit(name, color) {
this.name = name;
this.color = color;
}
var apple = new Fruit("apple", "red");

// (apple != null)
apple instanceof Object // true
apple instanceof Array // false

三、使用 Object.prototype.toString.call()判斷

call()方法可以改變this的指向,那么把Object.prototype.toString()方法指向不同的數(shù)據(jù)類型上面,返回不同的結(jié)果

function _typeof(obj){
var s = Object.prototype.toString.call(obj);
return s.match(/[object (.*?)]/)[1].toLowerCase();
};

_typeof([12,3,343]);
"array"

_typeof({name: 'zxc', age: 18});
"object"

_typeof(1);
"number"

_typeof("1");
"string"

_typeof(null);
"null"

_typeof(undefined);
"undefined"

_typeof(NaN);
"number"

_typeof(Date);
"function"

_typeof(new Date());
"date"

_typeof(new RegExp());
"regexp"

文章題目:js判斷數(shù)據(jù)類型
本文來源:http://bm7419.com/article18/geiedp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、電子商務(wù)、網(wǎng)站設(shè)計公司、自適應(yīng)網(wǎng)站商城網(wǎng)站、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)