判斷方法:1、使用toString()來判斷;2、使用“obj.constructor === Object”來判斷;3、使用“ypeof obj === Object”來判斷;4、利用instanceof關(guān)鍵字來判斷。
本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。
1、toString() 第一選擇
let obj = {} Object.prototype.toString.call(obj) === '[Object Object]'
2、constructor
let obj = {} obj.constructor === Object
【推薦學(xué)習(xí):js基礎(chǔ)教程】
3、instanceof
注意:使用instanceof對數(shù)組進(jìn)行判斷也是對象
let obj = {} obj instanceof Object //true let arr = [] arr instanceof Object //true
4、typeof
let obj = {} typeof obj === Object // 根據(jù)typeof判斷對象也不太準(zhǔn)確 表達(dá)式 返回值 typeof undefined 'undefined' typeof null 'object' typeof true 'boolean' typeof 123 'number' typeof "abc" 'string' typeof function() {} 'function' typeof {} 'object' typeof [] 'object'