其他分享
首页 > 其他分享> > 冻结对象与解冻对象

冻结对象与解冻对象

作者:互联网

冻结对象:

var pizza = {
    name: 'Peri Peri',
    Topping: 'Prawn'
};

Object.freeze(pizza);

// Can't change the name of the object because it's frozen
pizza.name = 'Hawaiian';

解冻对象:

var pizza = {
    name: 'Peri Peri',
    Topping: 'Prawn'
};

Object.freeze(pizza);
pizza.name = 'Hawaiian';
console.log(pizza);
// { name: 'Peri Peri', Topping: 'Prawn' }

pizza = JSON.parse(JSON.stringify(pizza));  // Clones the object

pizza.name = 'Hawaiian';
console.log(pizza);
// { name: 'Hawaiian', Topping: 'Prawn' }

 

标签:冻结,Topping,name,Hawaiian,对象,Peri,Prawn,解冻,pizza
来源: https://www.cnblogs.com/webljl/p/14355714.html