類型判斷----小白講解typeof,instanceof,Object.prototype.toString.call()
阿新 • • 發佈:2018-08-29
類型判斷 aso prototype 時間 all str 第一個 ima 16px 1.typeof只能判斷基本類型數據,
例子:
typeof 1 // "number" typeof ‘1‘ // "string" typeof true // "boolean" typeof {} // "object" typeof [] // "object" typeof function(){} // "function" typeof undefined // "undefined" typeof null // "object",註意!!!
2.instanceof let obj = new Object() obj instanceof Object // true 註意:instanceof左側必須是對象,如果不是直接返回false, 例如:
let num = 1 numinstanceof Number // false num = new Number(1) num instanceof Number // true
3.最好的類型判斷方法:Object.prototype.toString.call(需要判斷的值)
console.log(Object.prototype.toString.call(Object) === "[object Object]"); console.log(Object.prototype.toString.call("jerry"));//[object String] console.log(Object.prototype.toString.call(12));//[object Number] console.log(Object.prototype.toString.call(true));//[object Boolean] console.log(Object.prototype.toString.call(undefined));//[object Undefined] console.log(Object.prototype.toString.call(null));//[object Null] console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]console.log(Object.prototype.toString.call(function(){}));//[object Function] console.log(Object.prototype.toString.call([]));//[object Array] console.log(Object.prototype.toString.call(new Date));//[object Date] console.log(Object.prototype.toString.call(/\d/));//[object RegExp] function Person(){}; console.log(Object.prototype.toString.call(new Person));//[object Object]
為什麽這樣就能區分呢? toString方法返回反映這個對象的字符串。 那為什麽不直接用obj.toString()呢?
console.log("jerry".toString());//jerry console.log((1).toString());//1 console.log([1,2].toString());//1,2 console.log(new Date().toString());//Wed Dec 21 2016 20:35:48 GMT+0800 (中國標準時間) console.log(function(){}.toString());//function (){} console.log(null.toString());//報錯 console.log(undefined.toString());//報錯
同樣是檢測對象obj調用toString方法,obj.toString()的結果和Object.prototype.toString.call(obj)的結果不一樣,這是為什麽? 這是因為toString為Object的原型方法,而Array ,function等類型作為Object的實例,都重寫了toString方法。 不同的對象類型調用toString方法時,根據原型鏈的知識,調用的是對應的重寫之後的toString方法(function類型返回內容為函數體的字符串,Array類型返回元素組成的字符串.....),而不會去調用Object上原型toString方法(返回對象的具體類型),所以采用obj.toString()不能得到其對象類型,只能將obj轉換為字符串類型;因此,在想要得到對象的具體類型時,應該調用Object上原型toString方法。 驗證方法:將數組的toString方法刪除
var arr=[1,2,3]; console.log(Array.prototype.hasOwnProperty("toString"));//true console.log(arr.toString());//1,2,3 delete Array.prototype.toString;//delete操作符可以刪除實例屬性 console.log(Array.prototype.hasOwnProperty("toString"));//false console.log(arr.toString());//"[object Array]"
刪除了Array的toString方法後,同樣再采用arr.toString()方法調用時,不再有屏蔽Object原型方法的實例方法,因此沿著原型鏈,arr最後調用了Object的toString方法,返回了和Object.prototype.toString.call(arr)相同的結果。 封裝: 1.原理:截取[object type]------slice(8,-1)從第下標8開始截取,到下標倒數第一個 2.封裝成一個函數: //返回傳遞給他的任意對象的類 function isClass(o){ return Object.prototype.toString.call(o).slice(8,-1); } 3.使用:
參考:
https://www.cnblogs.com/youhong/p/6209054.html
https://www.cnblogs.com/Yance/p/7655320.html
類型判斷----小白講解typeof,instanceof,Object.prototype.toString.call()