【发布时间】:2015-01-20 16:40:46
【问题描述】:
我正在学习 JavaScript 和 Node.js,我对 Object.getOwnPropertyDescriptor() 函数有疑问。考虑以下顶级代码:
var rectangle = {
width: 10,
height: 5,
get area() {
return this.width * this.height;
}
};
Object.prototype.x = 5;
var areaPropDesc = Object.getOwnPropertyDescriptor(rectangle, "area");
for (var attr in areaPropDesc) {
console.log("areaPropDesc["+attr+"] is: "+areaPropDesc[attr]);
}
当我执行上面的代码时,输出如下:
areaPropDesc[get] is: function area() {
return this.width * this.height;
}
areaPropDesc[set] is: undefined
areaPropDesc[enumerable] is: true
areaPropDesc[configurable] is: true
areaPropDesc[x] is: 5
到底为什么x 属性包含在area 属性的属性描述符对象中?!
【问题讨论】:
标签: javascript node.js prototype javascript-objects