JavaScript 中的 freeze 和 seal 和有什么不一样?
freeze和seal都用于在 JavaScript 中创建不可扩展的对象,但它们之间有很多不同之处。 Object.seal()允许更改对象的现有属性,而Object.freeze()不允许这样做。 Object.freeze()使对象对所有事物都免疫,即使无法进行微小的更改。 Object.seal()防止删除现有属性,但不能防止它们受到外部更改。句法:
Object.freeze(objectname);
示例 1:描述Object.seal()的实现。
javascript
javascript
输出:
20
示例 1描述了如何使用Object.seal()创建一个不可扩展的对象,但这并不能阻止对象的值被更改,并且可以看到该值被更新为 20。语法:
Object.seal(objectname);
示例 2:描述Object.freeze()的实现。
javascript
输出:
10
示例 2描述了如何使用Object.freeze()创建不可扩展的对象,但阻止更改对象的现有值并给出 10 作为输出。
让我们以表格形式查看差异 - : Its syntax is -: Object.freeze(object) Its syntax is -: Object.seal(object) Its supported browsers are -: Chrome , Internet Explorer, Safari, Microsoft Edge, Opera Firefox Its supported browsers are -: Chrome , Internet Explorer, Safari, Microsoft Edge, Opera, Firefox freeze seal 1. freeze() is a javascript object which is used to prevent the object from adding new properties seal() is a Javascript method which is used to make properties of an object non-configurable. 2. It is also used so that the current existing properties should not be modified It is also used so that the new properties does not gets added 3. 4. It takes a parameter as an object It takes parameters as an object 5. Its return type is of the object type. Its return type is of the sealed object type. 6.