JS: Object Literal Expression vs Object.Create

By Xah Lee. Date: .

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.log(Reflect.getPrototypeOf(xx) === Reflect.getPrototypeOf(yy));

console.log(Object.isExtensible(xx));
console.log(Object.isExtensible(yy));

console.log(
  JSON.stringify(Object.getOwnPropertyDescriptor(xx, "kk")) ===
    JSON.stringify(Object.getOwnPropertyDescriptor(yy, "kk")),
);

JavaScript. Object and Inheritance