检测对象或数组

判断是否为对象

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值