Consider the following chunk of code:
function Bar() {
var _self = this;
this.addPin = function (param1) {
console.log('addPin');
}
function endPin(param1) {
console.log('endPin');
}
_self.addPin(1);
this.addPin(1);
addPin(1); // failse
endPin(2);
this.endPin(2); // fails
}
var foo = new Bar();
foo.addPin(1); // works
foo.endPin(1); // does not work
From what I understand from playing with this code, the function declaration of endPin
effectively makes it a private method to any outside callers. While this.methodHere
type of declaration makes it a public method.
What I am not clear on is the usage of this
in the class itself. this
refer to the instance of the class. Why, in that case, can't I use this.endPin
inside the class. Conversely, why do I have to use this.methodHere
syntax when referring to addPin
(which was declared using this.methodHere
syntax).
Finally the difference between this
and _self
- I've read that _self preserves the original state of the class (or this
). How does it do that if it's a reference supposedly pointing to the same memory location? How is this
different from _self
in this particular example?