如何访问javascript对象中的外部对象

时间:2013-05-03 08:20:20

标签: javascript

假设代码如。我想在某些函数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);
   }
}

1 个答案:

答案 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);
   }
}