Difference between freeze and seal

后端 未结 8 1009
孤独总比滥情好
孤独总比滥情好 2020-12-07 08:00

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 u

8条回答
  •  误落风尘
    2020-12-07 08:14

    You can now force a single object property to be frozen instead of freezing the whole object. You can achieve this with Object.defineProperty with writable: false as a parameter.

    var obj = {
        "first": 1,
        "second": 2,
        "third": 3
    };
    Object.defineProperty(obj, "first", {
        writable: false,
        value: 99
    });
    

    In this example, obj.first now has its value locked to 99.

提交回复
热议问题