javascript - 为什么我可以写一个像 "c = { get a(){} }"这样的对象?

标签 javascript

我写的时候

var c = { get a(){} };

结果是 c 对象具有“get a”和“set a”属性。

为什么?

已在 Chrome 和 Firefox 中测试。

最佳答案

Sometimes it is desirable to allow access to a property that returns a dynamically computed value, or you may want reflect the status of an internal variable without requiring the use of explicit method calls. In JavaScript, this can be accomplished with the use of a getter. It is not possible to simultaneously have a getter bound to a property and have that property actually hold a value, although it is possible to use a getter and a setter in conjunction to create a type of pseudo-property.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/get

关于javascript - 为什么我可以写一个像 "c = { get a(){} }"这样的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19025780/

相关文章:

javascript - 如何使HTML中的输入框只接受IP地址

javascript - 如何将数组元素附加到div?

javascript - jQuery 文本替换

javascript - 是否可以使用 <p> 标签将内联引号附加到包含多个段落的 block 引用?

javascript - ES6 : Accessing to class'es 'this' from inner local function

JavaScript - 如何转换 unicode 字符?英语数字到波斯语数字

用于更改列表项的内部 dom 元素的 Javascript 有效方法

javascript - jQuery 将 cas 更改为多个 div

javascript - 将地理坐标 (lat, lng) 映射到 map (x, y)

php - 加载页面时如何为html标签设置类?