172, js determined several methods empty object

 

First, the object into a string comparison 
This is the most readily occur mainly used the JSON.stringify () method of the strong object transfer: 
var obj = {}; 
the console.log (the JSON.stringify (obj) == " {} " )   


two, for ... in loop 
used for in seq loop can traverse all the properties determined whether the object object is empty: 
var obj = {}; 
function isEmptyObject (obj) { 
    for ( var Key in obj) {
         return  to false 
    }; 
    return  to true 
}; 
the console.log (isEmptyObject (obj)); 


three, Object.getOwnPropertyNames () 
all the property names in the property itself (not including but not enumerated attribute as the attribute name Symbol value) of an array. 
var obj = {};
the console.log (Object.getOwnPropertyNames (obj) .length == 0 ); // to true 


four, Object.keys () 
array can be enumerated attributes of 
var obj = {};
 var Array = Object.keys (obj); 
console.log (array.length == 0 ); // to true 


original link: HTTPS: // blog.csdn.net/watercatmiao/article/details/84261015

 

 

 

Guess you like

Origin www.cnblogs.com/gushixianqiancheng/p/12166672.html