博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
object.freeze_Object.freeze:不可变对象
阅读量:2513 次
发布时间:2019-05-11

本文共 1366 字,大约阅读时间需要 4 分钟。

object.freeze

One of my favorite part of JavaScript has always been mutability of objects.  I loved that MooTools and likewise frameworks could modify native prototypes to enhance them with functionality we knew the language need; in fact, I credit MooTools with pushing the web forward.

我最喜欢JavaScript部分之一一直是对象的可变性。 我喜欢MooTools和类似的框架可以修改本机原型,以使用我们知道语言需要的功能来增强它们。 实际上,我认为MooTools推动了网络的发展。

There are cases, however, where you don't want an object to be modifiable; you don't want values for existing properties to be changed, added, or removed.  That's where Object.freeze can help -- with Object.freeze you can create immutable objects you can trust!

但是,在某些情况下,您不希望对象是可修改的。 您不希望更改,添加或删除现有属性的值。 这就是Object.freeze可以提供帮助的地方-通过Object.freeze您可以创建可以信任的不可变对象!

const obj = Object.freeze({    x: 1,    y: 2});// None of these do anythingobj.x = 8; // { x: 1, y: 2}delete obj.x; // { x: 1, y: 2}obj.z = 3;  // { x: 1, y: 2}

Object.freeze is a welcomed addition to JavaScript and a necessary one.  If you have objects whose integrity you want to secure, immutability is required.  Object.seal provides similar functionality without the ability to freeze values, so Object.freeze is your best choice when you want to lock down an object!

Object.freeze是JavaScript的受欢迎补充,也是必需的。 如果您要保护其完整性的对象,则必须具有不变性。 Object.seal提供了类似的功能,但没有冻结值的功能,因此当您想要锁定对象时, Object.freeze是您的最佳选择!

翻译自:

object.freeze

转载地址:http://vuvwd.baihongyu.com/

你可能感兴趣的文章
Python 网络编程
查看>>
C# EF Code First Migrations数据库迁移
查看>>
将java保存成.xml文件
查看>>
SQl server更新某阶段的匹配关系。
查看>>
go语言练习
查看>>
org.apache.jasper.JasperException: Unable to compile class for JSP
查看>>
centos中的配置文件 分类: B3_LINUX ...
查看>>
1.找两个数下标Two Sum
查看>>
牛客~~wannafly挑战赛19~A 队列
查看>>
MYSQL GTID使用运维介绍(转)
查看>>
5 -- Hibernate的基本用法 --2 2 Hibernate的数据库操作
查看>>
RAID
查看>>
Jquery.Sorttable 桌面拖拽自定义
查看>>
PSP
查看>>
身份证的最准确的正则表达式,绝对让你吃惊啊!
查看>>
14.python读写Excel
查看>>
MySQL备份类别
查看>>
JNI数据类型(转)
查看>>
mysql 主从数据同步
查看>>
ContentType的一些值
查看>>