【问题标题】:What is "accessor function"?什么是“访问器功能”?
【发布时间】:2014-10-12 23:06:00
【问题描述】:

在标准 ECMA-262 版本的 section 4.3.26 中:

根据属性的形式,可以表示值 直接作为数据值(原始值、对象或 函数对象)或间接通过一对访问函数。

我不明白“存取函数”是什么意思,也没有在规范中找到存取函数的定义。然后我在网上搜索。在我看来,访问器函数的意思是“getter”。但我还是不明白,为什么属性值是“由一对访问器函数”表示的?任何人都可以用例子来说明这一点吗?谢谢!

【问题讨论】:

  • 我相信这里的访问者是指getter和setter。

标签: javascript


【解决方案1】:

“一对存取函数”是getter和setter。

Documentation and example:

var o = {}; // Creates a new object

// Example of an object property added with defineProperty with an accessor property descriptor
var bValue = 38;
Object.defineProperty(o, 'b', {
  get: function() { return bValue; },
  set: function(newValue) { bValue = newValue; },
  enumerable: true,
  configurable: true
});

【讨论】:

  • 您可以为位于宿主对象之外的变量定义 getter 和 setter,这真是一个不错的细节。
【解决方案2】:

访问器属性是根据 getter 和 setter 定义的属性,而不是可以写入的存储值。 “访问函数对”表示 getter 和 setter 函数。

更多信息请访问section §8.6

对象是属性的集合。每个属性要么是 命名数据属性、命名访问器属性或内部属性:

  • 命名数据属性将名称与 ECMAScript 语言相关联 值和一组布尔属性。
  • 命名访问器属性将名称与一个或两个访问器相关联 函数和一组布尔属性。访问器函数 用于存储或检索 ECMAScript 语言值 与属性相关联。
  • 内部属性没有名称,不能通过以下方式直接访问 ECMAScript 语言运算符。内部属性的存在纯粹是为了 规范目的。

Section § 8.6.1:

命名访问器属性将名称与下表中列出的属性相关联:

Attribute| Value     | Description
 Name    |  Domain   |
---------+-----------|---------------------------------------------------------
[[Get]]  | Object or | If the value is an Object it must be a function Object.
         | Undefined | The function’s [[Call]] internal method (8.6.2) is
         |           | called with an empty arguments list to return the
         |           | property value each time a get access of the property is 
         |           | performed.
         |           |
[[Set]]  | Object or | If the value is an Object it must be a function Object.
         | Undefined | The function’s [[Call]] internal method (8.6.2) is
         |           | called with an arguments list containing the assigned
         |           | value as its sole argument each time a set access of the
         |           | property is performed. The effect of a property's
         |           | [[Set]] internal method may, but is not required to,
         |           | have an effect on the value returned by subsequent calls
         |           | to the property's [[Get]] internal method.
         |           |
[[Enume- | Boolean   | If true, the property is to be enumerated by a for-in
 rable]] |           | enumeration (see 12.6.4). Otherwise, the property is
         |           | said to be non-enumerable.
         |           |
[[Confi- | Boolean   | If false, attempts to delete the property, change the
gurable]]|           | property to be a data property, or change its attributes
         |           | will fail.

【讨论】:

    【解决方案3】:

    一对存取函数指的是gettersetter。您可以间接访问对象中的某些值,例如:

    var person =
    {
        get Name()
        {
            return this.name;
        },
        set Name(value)
        {
            this.name = value;
        }
    };
    
    person.Name = "X";
    console.log(person.Name); // X
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-20
      • 2012-03-17
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多