首先我們來了解一下js中的數(shù)據(jù)類型包含哪些。
一嘴瓤、js數(shù)據(jù)類型
1.Number(數(shù)字型)
2.String(字符串行)
3.Boolean(布爾型)
4.Object (引用類型/對象類型)
5.Null(空值)
6.Undefined(未定義值)
除Object為對象(引用)類型之外舷嗡,其他都是原始(值)類型轴猎。
javascript是以O(shè)bject為基礎(chǔ)的語言,除基本數(shù)據(jù)類型外进萄,其他所有的引用數(shù)據(jù)類型捻脖,本質(zhì)上都是Object。
二中鼠、類型識別的幾種方式
1.typeof
typeof是一個(gè)一元運(yùn)算可婶,放在一個(gè)運(yùn)算數(shù)之前,運(yùn)算數(shù)可以是任意類型援雇。
它返回值是一個(gè)字符串矛渴,該字符串說明運(yùn)算數(shù)的類型。typeof一般只能返回如下幾個(gè)結(jié)果:
number,boolean,string,function,object,undefined
console.log(typeof 'abc'); //string
console.log(typeof 1 ); //number
console.log(typeof true ); //boolean
console.log(typeof undefined ); //undefined
console.log(typeof null ); //object
console.log(typeof {name: 'moon'} ); //object
console.log(typeof function () {} ); //function
console.log(typeof [] ); //object
console.log(typeof new Date() ); //object
console.log(typeof /\d/ ); //object
function Person() {}
console.log(typeof new Person() ); //object
小結(jié):
- typeof可以識別原始類型(Null除外)熊杨,Null類型會被識別成object
- 不能識別具體的對象類型(Function除外)
2.instanceof
instanceof 用于判斷一個(gè)變量是否某個(gè)對象的實(shí)例(一個(gè)實(shí)例是否屬于某一類型)曙旭,用來識別對象類型。
或者說晶府,對象與構(gòu)造函數(shù)在原型鏈上是否有關(guān)系桂躏。
//能夠判別內(nèi)置對象
var a=new Array();
console.log(a instanceof Array); //true
console.log(a instanceof Object);//true.(因?yàn)锳rray是object的子類)
//能夠判別自定義對象類型
function test(){};
var a=new test();
console.log(a instanceof test); //true
//不能判別原始數(shù)據(jù)類型
console.log(1 instanceof Number); //false
console.log('moon' instanceof String); //false
小結(jié):
- 可以判別所有對象類型,包括內(nèi)置對象和自定義對象
- 不能判別原始類型
3.Constructor
查看對象的構(gòu)造函數(shù)
//判別原始類型
console.log('moon'.constructor === String); //true
console.log((1).constructor === Number); //true,注意這里1要加上括號
console.log(true.constructor === Boolean); //true
//判別內(nèi)置對象
console.log([].constructor === Array ); //true
//判別自定義對象
function Aaa(){
}
var a1 = new Aaa();
console.log( a1.constructor ); //Aaa
小結(jié):
- 判別原始類型(Undefined和Null除外川陆,因?yàn)樗鼈儧]有構(gòu)造函數(shù))
- 判別內(nèi)置對象類型
- 判別自定義對象類型
獲取對象構(gòu)造函數(shù)名稱的方法:
function getConstructorName(obj){
return (obj===undefined||obj===null)?
obj:
(obj.constructor&&obj.constructor.toString().match(/function\s*([^(]*)/)[1]);
}
說明:
1.入?yún)bj如果是undefined和null剂习,則返回其自身;
2.入?yún)bj如果是其他值,則返回obj.constructor&&obj.constructor.toString().match(/function\s([^(])/)[1]的結(jié)果;
1)obj.constructor //保證這個(gè)對象存在構(gòu)造函數(shù)较沪,保證&&后面的語句正常執(zhí)行鳞绕。
2)obj.constructor.toString(); //”function Number(){[native code]}”,構(gòu)造函數(shù)的名稱,這里假設(shè)為Number()對象尸曼。
3).match(/function\s([^(])/)[1]; //”Number” 们何,通過.match方法獲取字符串中的子字符串
4.Object.prototype.toString.call()或{}.prototype.toString.call()
在使用時(shí)Object.prototype.toString.call(xxx)會返回類似[object String]的字符串給我們,用它就可以很好的判斷類型了控轿。
//先把方法封裝成一個(gè)函數(shù)
function type(obj) {
return Object.prototype.toString.call(obj).slice(8,-1);
}
//字符串截取我們需要的部分
console.log(type('moon')); //String
console.log(type(1)); //Number
console.log(type(true)); //Boolean
console.log(type(undefined)); //Undefined
console.log(type(null)); //Null
console.log(type({})); //Object
console.log(type([])); //Array
console.log(type(new Date())); //Date
console.log(type(/\d/)); //RegExp
console.log(type(function () {})); //Function
function Person() {
}
console.log(type(new Person())); //Object
小結(jié):
- 可以識別所有原始類型和內(nèi)置對象類型
- 不能識別自定義對象類型
5.總結(jié)
沒有完美的判斷類型的方法冤竹,但是我們可以根據(jù)需求自行選取合適的。
最后來一個(gè)練習(xí)題吧~
函數(shù)myType用于根據(jù)輸入?yún)?shù)返回相應(yīng)的類型信息茬射。
語法如下:
var str = myType (param);
使用范例如下:
myType (1); 返回值: "number"
myType (false); 返回值: "boolean"
myType ({}); 返回值: "object"
myType ([]); 返回值:" Array"
myType (function(){}); 返回值:"function"
myType (new Date()); 返回值: "Date"
請寫出函數(shù)myType的實(shí)現(xiàn)代碼鹦蠕。
function myType(param){
if(typeof param=="object"){
return Object.prototype.toString.call(param).slice(8,-1)=="Object"?
"object":
Object.prototype.toString.call(param).slice(8,-1);
}else{
return typeof param;
}
}