Answer
Why doesn't Object.create clone properties as expected?
Because Object.create isn't supposed to clone properties.
JavaScript objects are tricky
If we look at the documentation of Object.create, it says:
The Object.create() method creates a new object, using an existing object as the prototype of the newly created object.
Besides its own properties, each object in JavaScript also inherits properties from its prototype.
Now, the tricky part is – inherited properties are only visible in some cases. For example:
- Property accessor –
y.a or y['a'] – ✅ sees inherited properties
console.log(y.a); // outputs 1
Object.keys, Object.values, Object.entries – ❌ doesn't see inherited properties
console.log(Object.keys(y)); // outputs []
These concepts are much more comprehensively covered in Enumerability and ownership of properties.
For a more thorough explanation of prototypes, see Object prototypes.
Why does JSFiddle print it differently than Chrome?
Apparently, JSFiddle's implementation of console enumerates inherited properties, whereas Chrome's console implementation doesn't.
{ "a": 1 }.. for me.Object.createdoesn't clone anything. It creates a new (empty) object that inherits.