2

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!

AKG
  • 2,936
  • 5
  • 27
  • 36
  • 1
    4. way is the way to go, by using hi you are accessing trough instance instead of parent closure (that). – Goran.it Feb 23 '14 at 10:08
  • 1
    There should be no output at all since you never call `Loop`. One problem is that Loop is meant to be called as a function and not as a constructor so it should not start with a capital L. The other is that you set prototype function in the constructor body, this means that you don't know the difference between the shared prototype object and instance specific code that's run in the constructor body. Maybe the following answer can help you out: http://stackoverflow.com/a/16063711/1641941 Prototype is not that easy to understand so please feel free to ask about what isn't clear. – HMR Feb 23 '14 at 11:44
  • @HMR - I've indeed not quite understood prototypes and the post you linked to is one of the best written answers on SO. Thank you very much. – AKG Feb 23 '14 at 12:01
  • Thank you, it helped me while writing it and trying to improve the answer helped me a lot to understand this better. – HMR Feb 23 '14 at 16:16

2 Answers2

2

Normally prototype functions should be defined outside the constructor function and this.x is the correct way to access the variable.

var Setup = function() {
    this.x = 60;
};

Setup.prototype.Loop = function() {
    console.log(this.x);
};

(new Setup()).Loop();    // 60
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
1

Prototype should be set outside of constructor to be available at the time you create a new instance.

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();
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • Thanks @dfsq - however, I've found it nested like I did in a demo code by someone else. Anyway, is there any major difference between setting the prototype outside the constructor and nesting it the way I did? – AKG Feb 23 '14 at 10:12