0

Assume code like. I want to access i and j in some function somefunc. How will this be achieved?

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
  • Did you try this.i and this.j? If somefunc() doesn't get called in a different context (with call() or so) it should be the object itself. Commented May 3, 2013 at 8:24

1 Answer 1

1

They can be accessed through this.i and 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);
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.