//1. console.log(typeof(a));//undefined console.log(typeof(undefined));//undefined //2. console.log(typeof(1));//number console.log(typeof(NaN));//number //3. console.log(typeof(function(){}));//function //4. console.log(typeof(''));//string //5. console.log(typeof(true));//boolean //6. console.log(typeof({}));//object console.log(typeof(new Object));//object console.log(typeof([]));//object console.log(typeof(null));//object console.log(typeof(/\W/));//object console.log(typeof(new Date));//object
一、typeof的值有6個,分別是undefined、number、function、string、boolean、object數組
二、引用類型(Object、Array、function、Date)除了函數,值都爲object函數
//1. console.log(Object.prototype.toString.call(1));//[object Number] console.log(Object.prototype.toString.call(NaN));//[object Number] //2. console.log(Object.prototype.toString.call(function(){}));//[object Function] //3. console.log(Object.prototype.toString.call(''));//[object String] //4. console.log(Object.prototype.toString.call(true));//[object Boolean] --------------------------------------- //5. console.log(Object.prototype.toString.call({}));//[object Object] console.log(Object.prototype.toString.call(new Object));//[object Object] //6 console.log(Object.prototype.toString.call([]));//[object Array] //7 console.log(Object.prototype.toString.call(null));//[object Null] //8 console.log(Object.prototype.toString.call(/\W/));//[object RegExp] //9 console.log(Object.prototype.toString.call(new Date));//[object Date]>
console.log(Object.prototype.toString.call({}).split(" ")[1].substring(0,Object.prototype.toString.call({}).split(" ")[1].length-1));//Array
一、Object.prototype.toString.call的值有9個,分別是Number、Function、String、Boolean、Object、Array、Null、RegExp、Date。prototype
一、toString爲Object的原型方法,用Array ,function等類型做爲Object的實例,都重寫了toString方法。
二、不一樣的對象類型調用toString方法時,調用的是對應的重寫以後的toString方法,而不會去調用Object上原型toString方法,因此採用obj.toString()不能獲得其對象類型,只能將obj轉換爲字符串類型。code
console.log([].constructor == Array);//true console.log({}.constructor == Array);//false console.log([] instanceof Array);//->true console.log({} instanceof Array);//->false
用instanceof檢測的時候,只要當前的這個類在實例的原型鏈上(能夠經過原型鏈__proto__找到它),檢測出來的結果都是true對象
console.log(1 instanceof Number);//false console.log([].constructor == Number);//false