Object.create (null) {} and create objects distinction

Original: https: //www.jianshu.com/p/43ce4d7d6151

Create object:

    Create an empty object has the following three methods:

var obj1 = {};
var obj2 = Object.create(null);
var obj3 = new Object();

Create an empty object distinction:

    To create a clean empty object, you should use Object.create (null) instead of the remaining two kinds.

   By doing Object.create (null), we can explicitly specify null as its prototype. So it has absolutely no property,

Even without a constructor, toString, hasOwnProperty properties, if so desired, these keys may be used in the data structure, without the need for determination by hasOwnProperty.

var obj1 = {};
var obj2 = Object.create(null);
var obj3 = new Object();

var con1 = obj1.constructor;
var con2 = obj2.constructor;
var con3 = obj3.constructor;

console.log(con1);//ƒ Object() { [native code] }
console.log(con2);//undefined
console.log(con3);// Object() { [native code] }

Example shows

const dirtyMap = {};
 const cleanMap the Object.create = ( null );
 const Key;
 for (Key in dirtyMap) {
   // . Avoid to the Check for iterating through the over Inherited Properties
   // impure object needs to be done to judge 
  IF (dirtyMap .hasOwnProperty (Key)) {    
    the console.log (Key + " -> " + dirtyMap [Key]); 
  } 
}

 

Guess you like

Origin www.cnblogs.com/psxiao/p/11517016.html