我在构造函数中有这段代码:
Object.defineProperty(this, "color", {
get : function() {
return color;
},
set : function(newVal) {
color = newVal;
this.path.attr("stroke", color);
}
});JSHint警告“颜色”未定义。在用defineProperty配置它之前,我是否应该以某种方式定义“颜色”?
(我曾尝试在this.color中使用“defineProperty”,但这会导致无限循环)
谢谢
发布于 2014-03-10 08:57:34
color确实是未定义的。你需要把信息储存在其他地方。
你可以通过关闭:
var color;
Object.defineProperty(this, "color", {
get : function() {
return color;
},
set : function(newVal) {
color = newVal;
this.path.attr("stroke", color);
}
});或者使用另一个不可枚举的属性(这样它就不会出现在for in上)和不可配置的属性(以避免重写):
Object.defineProperty(this, "_color", {
configurable: false,
writable: true,
enumerable: false
});
Object.defineProperty(this, "color", {
get : function() {
return this._color;
},
set : function(newVal) {
this._color = newVal;
this.path.attr("stroke", color);
}
});https://stackoverflow.com/questions/22295600
复制相似问题