JS: Object Literal Expression vs Object.Create

By Xah Lee. Date: . Last updated: .

Object Literal Expression vs Object.Create

{key:val}

is equivalent to

Object.create( Object.prototype, {key: {value:val, enumerable: true, configurable: true, writable: true}})

// check equivalence of object literal and Object.create

const xx = { "kk": 1 };

const yy = Object.create(
 Object.prototype,
 { "kk": { value: 1, enumerable: true, configurable: true, writable: true } },
);

console.assert(Reflect.getPrototypeOf(xx) === Reflect.getPrototypeOf(yy));

console.assert((Object.isExtensible(xx)) === true);
console.assert((Object.isExtensible(yy)) === true);

console.assert(
 JSON.stringify(Reflect.getOwnPropertyDescriptor(xx, "kk")) ===
  JSON.stringify(Reflect.getOwnPropertyDescriptor(yy, "kk")),
);

// Object.entries(Reflect.getOwnPropertyDescriptor(xx, "kk"))
// [ [ "value", 1 ], [ "writable", true ], [ "enumerable", true ], [ "configurable", true ] ]

JavaScript. Object and Inheritance