Reputation: 197
I can't understand why my object overwrite another frozen object, i try to create a new instance from my previous object and then freeze it. But anyways, it's overwrites itself even if it's frozen
Also, why i can change my check
object if it's const
JS is actaully weird
I have the code below
let obj = {check: "check"};
const check = Object.create(obj);
Object.freeze(check);
obj["second"] = "something";
// Expected result: obj = {check: "check", second: "something"}
// check = {check: "check"}
// Actual result: obj = {check: "check", second: "something"}
// check = {check: "check", second: "something"}
Upvotes: 0
Views: 70
Reputation: 622
The Object.create() method creates a new object, using an existing object as the prototype of the newly created object. The properties of the prototype are accessible to the child objects.
Source : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
You have to use Object.assign() if you want to clone an object.
Upvotes: 0
Reputation: 36574
Object.create()
doesnot clone the object you should use Object.assign
let obj = {check: "check"};
const check = Object.create(obj);
Object.seal(check);
obj["second"] = "something";
console.log(check)
Upvotes: 2