下面將對以下數據進行判斷它們的類型jquery
var bool = true var num = 1 var str = 'abc' var und = undefined var nul = null var arr = [1,2,3] var obj = {name:'haoxl',age:18} var fun = function(){console.log('I am a function')}
console.log(typeof bool); //boolean console.log(typeof num);//number console.log(typeof str);//string console.log(typeof und);//undefined console.log(typeof nul);//object console.log(typeof arr);//object console.log(typeof obj);//object console.log(typeof fun);//function
由結果可知typeof能夠測試出number
、string
、boolean
、undefined
及function
,而對於null
及數組、對象,typeof均檢測出爲object,不能進一步判斷它們的類型。
console.log(bool instanceof Boolean);// false console.log(num instanceof Number);// false console.log(str instanceof String);// false console.log(und instanceof Object);// false console.log(arr instanceof Array);// true console.log(nul instanceof Object);// false console.log(obj instanceof Object);// true console.log(fun instanceof Function);// true var bool2 = new Boolean() console.log(bool2 instanceof Boolean);// true var num2 = new Number() console.log(num2 instanceof Number);// true var str2 = new String() console.log(str2 instanceof String);// true function Person(){} var per = new Person() console.log(per instanceof Person);// true function Student(){} Student.prototype = new Person() var haoxl = new Student() console.log(haoxl instanceof Student);// true console.log(haoxl instanceof Person);// true
從結果中看出instanceof不能區別undefined和null,並且對於基本類型若是不是用new聲明的則也測試不出來,對因而使用new聲明的類型,它還能夠檢測出多層繼承關係。
undefined和null沒有contructor屬性
console.log(bool.constructor === Boolean);// true console.log(num.constructor === Number);// true console.log(str.constructor === String);// true console.log(arr.constructor === Array);// true console.log(obj.constructor === Object);// true console.log(fun.constructor === Function);// true console.log(haoxl.constructor === Student);// false console.log(haoxl.constructor === Person);// true
constructor不能判斷undefined和null,而且使用它是不安全的,由於contructor的指向是能夠改變的
console.log(Object.prototype.toString.call(bool));//[object Boolean] console.log(Object.prototype.toString.call(num));//[object Number] console.log(Object.prototype.toString.call(str));//[object String] console.log(Object.prototype.toString.call(und));//[object Undefined] console.log(Object.prototype.toString.call(nul));//[object Null] console.log(Object.prototype.toString.call(arr));//[object Array] console.log(Object.prototype.toString.call(obj));//[object Object] console.log(Object.prototype.toString.call(fun));//[object Function] function Person(){} function Student(){} Student.prototype = new Person() var haoxl = new Student() console.log(Object.prototype.toString.call(haoxl));//[object Object]
原理(摘自高級程序設計3):在任何值上調用 Object 原生的 toString() 方法,都會返回一個 [object NativeConstructorName] 格式的字符串。每一個類在內部都有一個 [[Class]] 屬性,這個屬性中就指定了上述字符串中的構造函數名。
可是它不能檢測非原生構造函數的構造函數名。
console.log($.type(bool));//boolean console.log($.type(num));//number console.log($.type(str));//string console.log($.type(und));//undefined console.log($.type(nul));//null console.log($.type(arr));//array console.log($.type(obj));//object console.log($.type(fun));//function function Person(){} function Student(){} Student.prototype = new Person() var haoxl = new Student() console.log($.type(haoxl));//object
$.type()內部原理就是用的Object.prototype.toString.call()