What is “accessor function”?

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 (a primitive value, an object, or a function object) or indirectly by a pair of accessor functions.

I didn't understand what "accessor function" means and I didn't find the definition of accessor function in the specification. Then I searched the web. It seemed to me that accessor function means "getter". But I still don't understand, why is the property the value be represented "by a pair of accessor functions"? Can anyone illustrate this with example(s)? Thanks!

Answers:

Answer

"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
});
Answer

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.
Answer

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.