Resources I’ve read
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
I have a simple script
const defaults = {
allowInputStyleChange: true
};
var entry = Object.create(defaults);
console.log(JSON.stringify(defaults));
console.log(JSON.stringify(entry));
This results in
{"allowInputStyleChange":true}
{}
I was expecting
{"allowInputStyleChange":true}
{"allowInputStyleChange":true}
Am I understanding how to implement it wrong?
2
Answers
However, if you do:
The problem you have is that
JSON.stringify
only shows the enumerable own property of an object.So
Object.create
is working according to what you’re expecting, it’s just thatJSON.stringify
does not show the prototypeObject.create()
creates a new object with the passing object’s prototype.JSON.stringify()
serializes the own enumerable properties of the object. Since,entry
does not have it’s own property, it returns{}
To get the properties of
defaults
included in the JSONString, you can useObject.assign()
: