I'm confused about this
keyword in JS like below. Why this.this_
is undefined in line 3) ? Any simple exalain for that ? In line 2) this
points to Window object - i was suprised, but it is acceptable for me - why is that ? The only working for me is to use this2_
variable, but i think that is not elegant. How to use this
in good,elegant approach to always work and points to O
object ?
var a = function(callback){
callback('test');
};
var O = function(){
this.val = 'abc';
var this2_ = this;
this.this_ = this;
this.f = function(){
console.log(this); // (1) this -> O object, that's OK
a(function(){
console.log(this); // (2) this -> Window object
console.log(this.this_); // (3) this.this_ -> undefined
console.log(this2_); // (4) this2_ -> O object
});
}
};
var o = new O();
o.f();