[英]How to access outer object in javascript objects
假设代码像。 我想在某些函数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);
}
}
可以通过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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.