码迷,mamicode.com
首页 > 编程语言 > 详细

[Javascript] Object.freeze() vs Object.seal()

时间:2016-07-25 01:48:53      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

let person = {
  firstName: "Zhentian",
  lastName: "Wan"
};

/*Object.freeze() makes object cannot be updated, added or deleted*/
let freezePerson = Object.freeze(person);
freezePerson.address="Finland"; // Cannot add property address, object is not extensiable.
console.log(freezePerson.firstName); // Zhentian
freezePerson.firstName = "John"; //Cannot assing to read only property ‘firstName‘

/*Seal, property can be udpated, but cannot be delted or added*/
let sealPerson = Object.seal(person);
sealPerson.firstName = "Answer"; // works!
console.log(sealPerson.firstName);
sealPerson.address="Finland"; // Cannot add property address, object is not extensible
console.log(sealPerson.address);

 

[Javascript] Object.freeze() vs Object.seal()

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5702064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!