Analyzing the case where the object is empty

A, Object.getOwnPropertyNames () method


This method is to use getOwnPropertyNames method Object object, access to the property name objects, stored into an array and returns an array of objects, we can be judged by judging length of the array whether this object is empty
Note: This method is not compatible with ie8, no remaining test browser
var Data = {};
var = Object.getOwnPropertyNames ARR (Data);
Alert (arr.length == 0); // to true

 

Second, the use Object.keys ES6 () method


4 and similar methods, a new method for ES6, the return value is an array composed of object attribute name
var Data = {};
var = Object.keys ARR (Data); // this returns an array of all the property names in the object
alert (arr.length == 0); // true no property, the array is empty

Guess you like

Origin www.cnblogs.com/zhouyu0001/p/11334669.html