how can I achieve below things in javascript
var parent = {
a: 5,
b: 6
}
var child = parent;
console.log(child.b); // 6
//now if I change the value of b
parent.b = 7
console.log(parent.b); //7
console.log(child.b); //7
//but I want to preserve the previous value of the object property it should not change now.it should show 6 instead of 7 in case of console.log(child.b)
parent.bnot to affect the value ofchild.b), cloning the object rather than just referring to it is the real problem. The OP's mention of inheritance and such is, I think, a red herring. (Not sure I fully agree with the closure, but I don't disagree enough to unclose it myself, esp. in light of theparent.b/child.bthing. :-) )