我写的时候
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/