假设代码如。我想在某些函数somefunc中访问i和j。这将如何实现?
var myObj = function(){
Object.defineProperty(this, 'j'){
get: function() { return 1;}
};
}
myObj.prototype = Object.create(null);
myObj.prototype={
constructor : myObj,
i : 1,
somefunc : function(){
console.log(i + j);
}
}
答案 0 :(得分:1)
可以通过this.i和this.j
访问它们var myObj = function(){
Object.defineProperty(this, 'j'){
get: function() { return 1;}
};
}
myObj.prototype = Object.create(null);
myObj.prototype={
constructor : myObj,
i : 1,
somefunc : function(){
console.log(this.i + this.j);
}
}