Creating new objects from frozen parent objects

前端 未结 4 1818
栀梦
栀梦 2020-12-05 19:45

This example creates an object, freezes it, and then creates a new object from the frozen object. If the second object tries to change the test property, it can\'t. It remai

4条回答
  •  臣服心动
    2020-12-05 19:59

    Use Object.assign

             var first = {
                test: 10
            };
            Object.freeze(first);
    
            //Create a second object from the first one and
            //try and change the new test property (you can't)
    
            var second = Object.assign({}, first, {
                test: 20
            });
            console.log(second.test); //20
    

提交回复
热议问题