javascript - ES5 规范对象类型

标签 javascript ecmascript-5

我正在通读 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.

有人可以澄清这个短语的确切含义(它的含义是什么?)? 我只看到了命名访问器属性 getset

最佳答案

规范说puttinggetting 是可能发生在属性上的两个 Action 。 Objects have the internal methods [[Put]] and [[Get]]与那些 Action 相对应。

[[Put]]是一种内部方法,存在于每个对象中,将值存储在对象的属性中。任何时候对属性执行赋值操作,都会导致环境执行 [[Put]]

[[Set]]是访问器属性描述符的内部属性。这是您在定义访问器属性的 set setter 函数时定义的内容。 (注意:[[Set]] 存在于属性描述符,而不是对象。并非所有属性都有 [[Set] ],但所有对象都有一个 [[Put]]。)

如果您尝试对作为访问器属性的对象属性执行 [[Put]],该操作将调用属性的 [[Set]] 函数,根据 [[Put]] 算法的第 5 步:

  1. 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/

相关文章:

javascript - 正则表达式 - 如何过滤数字和 "-"?

javascript - JSON+Node.js - 意外的 token o

javascript - 为什么 JavaScript 中命名函数内的函数名称不再引用函数本身?

javascript - 更改 props 后,componentDidMount 不断重复调用

javascript - 将 block 与对象初始值设定项区分开来

javascript - 在 Chrome 中,JS 绑定(bind)函数具有 null arguments.callee.caller

javascript - Summernote 字体系列和颜色不起作用

javascript - Chrome 使用 onload 和 load 函数抛出错误

javascript - 有没有一种方法可以在没有 ES6 类和像 Babel 这样的预编译工具的情况下创建 React 组件?

javascript - 在 Three.js 中创建漏洞