当前位置: 首页 > news >正文

检测对象或数组

判断是否为对象

typeof()

typeof   {}              //   object

instanceof()

使用 instanceof 就是判断一个实例是否属于某种类型。

const b = {};
console.log(a instanceof Object);       //true

弊端

更重要的一点是 instanceof 可以在继承关系中用来判断一个实例是否属于它的父类型。

console.log(Object instanceof Object);           //true 
console.log(Function instanceof Function);       //true 
console.log(Number instanceof Number);           //false 
console.log(String instanceof String);           //false 
console.log(Function instanceof Object);         //true 
console.log(Foo instanceof Function);            //true 
console.log(Foo instanceof Foo);                 //false

比较自定义对象

function Foo() {}
function Bar() {}
Bar.prototype = new Foo();

new Bar() instanceof Bar; // true
new Bar() instanceof Foo; // true

// 如果仅仅设置 Bar.prototype 为函数 Foo 本身,而不是 Foo 构造函数的一个实例。
Bar.prototype = Foo;
new Bar() instanceof Foo; // false

instanceof 比较内置类型
但是,不是通过构造函数创建的对象使用instanceof比较,那得到的,可能就不是你想要的结果。

new String('foo') instanceof String; // true
new String('foo') instanceof Object; // true

'foo' instanceof String; // false
'foo' instanceof Object; // false

constructor

const o = {};
console.log(o.constructor);            //function Object(){ [native code] }

Object.prototype.toString

When the toString method is called, the following steps are taken:
If the this value is undefined, return "[object Undefined]".
If the this value is null, return "[object Null]".
Let O be the result of calling ToObject passing the this value as the argument.
Let class be the value of the [[Class]] internal property of O.
Return the String value that is the result of concatenating the three Strings "[object ", class, and "]".
const b = {0:'Hello',1:'Howard'};
const c = 'Hello Howard';
Object.prototype.toString.call(b);     //"[object Object]"
Object.prototype.toString.call(c);     //"[object String]"

判断是否为数组

instanceof()

const a = [];
console.log(a instanceof Array);       //true

constructor

实例化的数组拥有一个constructor属性,这个属性指向生成这个数组的方法。

const a = [];
console.log(a.constructor);            //function Array(){ [native code] }
constructor属性是可以改写的,如果更改,那么使用这种方法就无法判断出是否为数组了

Object.prototype.toString

const a = ['Hello','Howard'];
Object.prototype.toString.call(a);     //"[object Array]"

Array.isArray()

const a = [];
const b = {};
Array.isArray(a);//true
Array.isArray(b);//false

修改constructor对象:

const a = [];
const b = {};
a.constructor = b.constructor;
Array.isArray(a);         //true

判断是否为空对象

for in

function isEmptyObject(obj){ 
    for (var key in person) {
      if (person.hasOwnProperty(key)) {
        return false;
      }
    }
    return true;
};

JSON.stringify()

var data = {};
JSON.stringify(data) == "{}"     //true

Object.getOwnPropertyNames()

var data = {};
var arr = Object.getOwnPropertyNames(data);
console.log(arr.length == 0);       //true

Object.keys()(ES6)

var data = {};
var arr = Object.keys(data);
console.log(arr.length == 0);       //true

相关文章:

  • Python--作业2--对员工信息文件,实现增删改查操作
  • BAT面试常的问题和最佳答案
  • MFS分布式文件系统服务搭建
  • redis系列:通过文章点赞排名案例学习sortedset命令
  • 自抗凝透析器研究取得系列进展
  • (转)visual stdio 书签功能介绍
  • 如何高效学习和工作(撸代码)
  • python代码-leetcode1 两数相加
  • WPF 简洁的主界面
  • PowerDesigner使用小总结
  • 用开源技术巧解代账公司开票据难题
  • mysql 主从同步详细配置教程
  • cURL error 60: SSL certificate problem...
  • OSPF动态路由重分发实验
  • 数据库名称
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • [数据结构]链表的实现在PHP中
  • 《深入 React 技术栈》
  • 【从零开始安装kubernetes-1.7.3】2.flannel、docker以及Harbor的配置以及作用
  • Computed property XXX was assigned to but it has no setter
  • MYSQL 的 IF 函数
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • vue-router 实现分析
  • vue--为什么data属性必须是一个函数
  • 关于Java中分层中遇到的一些问题
  • 回顾2016
  • 前端相关框架总和
  • 全栈开发——Linux
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 通过获取异步加载JS文件进度实现一个canvas环形loading图
  • 微信小程序:实现悬浮返回和分享按钮
  • 学习笔记TF060:图像语音结合,看图说话
  • 因为阿里,他们成了“杭漂”
  • 基于django的视频点播网站开发-step3-注册登录功能 ...
  • ​学习一下,什么是预包装食品?​
  • !!java web学习笔记(一到五)
  • # Panda3d 碰撞检测系统介绍
  • #我与Java虚拟机的故事#连载10: 如何在阿里、腾讯、百度、及字节跳动等公司面试中脱颖而出...
  • %check_box% in rails :coditions={:has_many , :through}
  • (04)odoo视图操作
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (echarts)echarts使用时重新加载数据之前的数据存留在图上的问题
  • (windows2012共享文件夹和防火墙设置
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (原创)Stanford Machine Learning (by Andrew NG) --- (week 9) Anomaly DetectionRecommender Systems...
  • (原創) 如何動態建立二維陣列(多維陣列)? (.NET) (C#)
  • .NetCore部署微服务(二)
  • .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验
  • .net专家(张羿专栏)
  • .php文件都打不开,打不开php文件怎么办
  • /etc/apt/sources.list 和 /etc/apt/sources.list.d
  • @ModelAttribute注解使用
  • @RequestBody与@ResponseBody的使用