我正在通读 ES5 规范,对以下内容感到有些困惑:
8.6 The Object Type
... There are two kinds of access for named (non-internal) properties: get and put, corresponding to retrieval and assignment, respectively.
有人可以澄清这个短语的确切含义(它的含义是什么?)?
我只看到了命名访问器属性 get
和 set
。
最佳答案
规范说putting 和getting 是可能发生在属性上的两个 Action 。 Objects have the internal methods [[Put]]
and [[Get]]
与那些 Action 相对应。
[[Put]]
是一种内部方法,存在于每个对象中,将值存储在对象的属性中。任何时候对属性执行赋值操作,都会导致环境执行 [[Put]]
。
[[Set]]
是访问器属性描述符的内部属性。这是您在定义访问器属性的 set
setter 函数时定义的内容。 (注意:[[Set]]
存在于属性描述符,而不是对象。并非所有属性都有 [[Set] ]
,但所有对象都有一个 [[Put]]
。)
如果您尝试对作为访问器属性的对象属性执行 [[Put]]
,该操作将调用属性的 [[Set]]
函数,根据 [[Put]]
算法的第 5 步:
If IsAccessorDescriptor(desc) is true, then
- a. Let setter be desc.[[Set]] which cannot be undefined.
- b. Call the [[Call]] internal method of setter providing O as the this value and providing V as the sole argument.
简而言之,[[Set]]
函数仅适用于访问器属性,而 [[Put]
可以应用于访问器或数据属性。
关于javascript - ES5 规范对象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32954533/