什么是“访问者功能”? [英] What is "accessor function"?
问题描述
在第4.3.26节中标准ECMA-262版本:
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不明白访问器功能是什么意思,我没有在规范中找到访问器功能的定义。然后我搜索了网络。在我看来,访问器功能意味着getter。但是我仍然不明白,为什么属性值是由一对存取函数表示的?任何人都可以通过示例来说明这一点吗?谢谢!
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!
推荐答案
一对存取函数是getter和setter。
"A pair of accessor functions" are the getter and the setter.
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
});
这篇关于什么是“访问者功能”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!