检测对象或数组

发布时间:2019-08-11 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了检测对象或数组脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

判断是否为对象

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.@L_777_7@.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

Object.getPrototypeOf()

Object.getPrototypeOf(a) === Array.prototype; 

判断是否为空对象

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

脚本宝典总结

以上是脚本宝典为你收集整理的检测对象或数组全部内容,希望文章能够帮你解决检测对象或数组所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。