Example 1
var Reptile = function () {
var reptile = this;
this.showBla = function() {
alert(reptile.bla);
}
}
var turtle = new Reptile();
turtle.bla = 'whatever';
turtle.showBla();
Example 2
var Reptile = function () {
this.showBla = function() {
alert(this.bla);
}
}
var turtle = new Reptile();
turtle.bla = 'whatever';
turtle.showBla();
Is example 1 legit? As it sometimes seems to screw things over to define “this” directly in the constructor…?!?
Yes, it is legit and is useful in cases where you may need to define a function inside a function that may be invoked in a way there “this” will pointer to something else. Books recommend naming this variable var that = this;