Object.create
var emptyObject = Object.create(null);
var emptyObject = Object.create(null);
var emptyObject = {};
var emptyObject = new Object();
区别:
var o; // create an object with null as prototype o = Object.create(null); o = {}; // is equivalent to: o = Object.create(Object.prototype); function Constructor(){} o = new Constructor(); // is equivalent to: o = Object.create(Constructor.prototype); // Of course, if there is actual initialization code in the Constructor function, the Object.create cannot reflect it // create a new object whose prototype is a new, empty object // and a adding single property 'p', with value 42 o = Object.create({}, { p: { value: 42 } }) // by default properties ARE NOT writable, enumerable or configurable: o.p = 24 o.p //42 o.q = 12 for (var prop in o) { console.log(prop) } //"q" delete o.p //false //to specify an ES3 property o2 = Object.create({}, { p: { value: 42, writable: true, enumerable: true, configurable: true } });
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create