What is “accessor function”?

后端 未结 3 1675
春和景丽
春和景丽 2021-01-23 21:34

In section 4.3.26 of the Standard ECMA-262 Edition:

Depending upon the form of the property the value may be represented either directly as a data value

相关标签:
3条回答
  • 2021-01-23 21:44

    "A pair of accessor functions" are the getter and the 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
    });
    
    0 讨论(0)
  • 2021-01-23 21:56

    An accessor property is one that is defined in terms of getters and setters, not as a stored value that might be written to. The "pair of accessor functions" denotes the getter and the setter function.

    More information on this can be found in section §8.6:

    An Object is a collection of properties. Each property is either a named data property, a named accessor property, or an internal property:

    • A named data property associates a name with an ECMAScript language value and a set of Boolean attributes.
    • A named accessor property associates a name with one or two accessor functions, and a set of Boolean attributes. The accessor functions are used to store or retrieve an ECMAScript language value that is associated with the property.
    • An internal property has no name and is not directly accessible via ECMAScript language operators. Internal properties exist purely for specification purposes.

    and Section § 8.6.1:

    A named accessor property associates a name with the attributes listed in the following table:

    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.
    
    0 讨论(0)
  • 2021-01-23 22:00

    A pair of accessor function are referring to getter and setter. You can indirectly access some value in your object, for example:

    var person =
    {
        get Name()
        {
            return this.name;
        },
        set Name(value)
        {
            this.name = value;
        }
    };
    
    person.Name = "X";
    console.log(person.Name); // X
    
    0 讨论(0)
提交回复
热议问题