JavaScript 判斷是否為對象


1、toString() 第一選擇

let obj = {}
Object.prototype.toString.call(obj) === '[Object Object]'

2、constructor

let obj = {}
obj.constructor === Object

3、instanceof

注意:使用instanceof對數組進行判斷也是對象

let obj = {}
obj instanceof Object  //true
let arr = []
arr instanceof Object  //true

4、typeof

let obj = {}
typeof obj === Object
// 根據typeof判斷對象也不太准確
表達式                       返回值
typeof undefined           'undefined'
typeof null                'object'
typeof true                'boolean'
typeof 123                 'number'
typeof "abc"               'string'
typeof function() {}       'function'
typeof {}                  'object'
typeof []                  'object'

原文:https://blog.csdn.net/zhangjing0320/article/details/81230170

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM