Opposite of Object.freeze or Object.seal in JavaScript

前端 未结 9 1338
无人共我
无人共我 2020-12-02 21:41

What is the opposite of Object.freeze or Object.seal? Is there a function that has a name such as detach?

9条回答
  •  一向
    一向 (楼主)
    2020-12-02 22:41

    You can unfreeze an array by using spread operator.

    //let suppose arr is a frozen array i.e. immutable
    var arr = [1, 2, 3];
    
    //if arr is frozen arr you cannot mutate any array referring to it
    var temp = arr;
    
    temp.push(4);  //throws an error "Cannot modify frozen array elements"
    
    //here mutableArr gets the elements of arr but not reference to it
    //hence you can mutate the mutableArr
    
    var mutableArr = [...arr];
    
    mutableArr.push(4);  //executes successfully 
    

提交回复
热议问题