开发者社区> 问答> 正文

Object.prototype.toString.call() 和 instanceOf 区别好坏

Object.prototype.toString.call() 和 instanceOf 和 Array.isArray() 区别好坏

展开
收起
茶什i 2019-11-21 15:30:37 1229 0
1 条回答
写回答
取消 提交回答
  • Object.prototype.toString.call()
    优点:这种方法对于所有基本的数据类型都能进行判断,即使是 null 和 undefined 。
    缺点:不能精准判断自定义对象,对于自定义对象只会返回[object Object]
    instanceOf
    优点:instanceof 可以弥补 Object.prototype.toString.call()不能判断自定义实例化对象的缺点。
    缺点: instanceof 只能用来判断对象类型,原始类型不可以。并且所有对象类型 instanceof Object 都是 true,且不同于其他两种方法的是它不能检测出 iframes。
    Array.isArray()
    优点:当检测 Array 实例时,Array.isArray 优于 instanceof ,因为 Array.isArray 可以检测出 iframes
    缺点:只能判别数组

    2019-11-21 15:30:53
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载