I have got the following function:
var Setup = function() {
var that = this;
this.x = 60;
Setup.prototype.Loop = function() {
console.log(this.x); // 1: returns 'undefined'
console.log(Setup.x); // 2: returns 'undefined'
console.log(hi.x); // 3: returns '60'
console.log(that.x); // 4: returns '60'
}
}
var hi = new Setup();
I am wondering what is the proper way to access the this.x
constructor from the Setup.prototype.Loop
function?
I have tried four ways (see above): only the third and fourth lines give me the result I want. Admittedly, line no. 3 is not the best way because it relies on the object hi
. Line no. 4 seems to be the best thing I can think of.
I got it to work but I am trying to learn the best way of doing things. Is there any other way or better way to do this?
Also, I suppose this is a question that must have been asked before but somehow I cannot find the words to find the question. Also, please forgive me should I have used some wrong terminology here and there.
Thanks for your replies!