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 = { dog: 1 };

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

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

console.assert(
 Reflect.isExtensible(xx),
);

console.assert(
 Reflect.isExtensible(yy),
);

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