在标准ECMA-262版本的section 4.3.26中:
根据房产的形式,可以表示价值 或者直接作为数据值(原始值,对象或者 函数对象)或间接由一对存取函数。
我不明白“访问器功能”是什么意思,我没有在规范中找到访问器功能的定义。然后我搜索了网络。在我看来,访问器功能意味着“getter”。但是我仍然不明白,为什么属性值是“由一对存取函数”表示的?任何人都可以通过示例来说明这一点吗?谢谢!
答案 0 :(得分:4)
访问者属性是根据getter和setter定义的属性,而不是可能写入的存储值。 “配对函数对”表示getter和setter函数。
有关此内容的更多信息,请参阅section §8.6:
对象是属性的集合。每个属性都是a 命名数据属性,命名访问器属性或内部属性:
- 命名数据属性将名称与ECMAScript语言相关联 值和一组布尔属性。
- 命名的访问者属性将名称与一个或两个访问者相关联 函数和一组布尔属性。访问器功能 用于存储或检索ECMAScript语言值 与财产有关。
- 内部属性没有名称,无法直接访问 ECMAScript语言运算符。内部属性纯粹存在 规范目的。
命名的访问者属性将名称与下表中列出的属性相关联:
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.
答案 1 :(得分:3)
"一对存取功能"是吸气剂和吸气剂。
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
});
答案 2 :(得分:1)