I just heard about the JavaScript methods freeze
and seal
, which can be used to make any Object immutable.
Here's a short example how to use it:
var o1 = {}, o2 = {};
Object.freeze(o2);
o1["a"] = "worked";
o2["a"] = "worked";
alert(o1["a"]); //prints "worked"
alert(o2["a"]); //prints "undefined"
What is the difference between freeze
and seal
? Can they increase performance?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…