Using the following bit of code:
function Node(){
....
function foo(request){
for (var name in this ) {
log(name +" is "+this[name]);
if(!(name == 'all' || typeof this[name] == 'function')){
request[name] = this[name];
}
}
return ...
};
}
I was surprised that when the private function foo is called this doesn’t seem to refer to the containing object (an instance of Node). Why is that ?
Of course I could have something like:
function Node(){
var ref = this;
....
}
and use ref as this in foo, but is there a way of declaring private methods for which this is a reference to the containing object?
‘this’ refers to the object used to call the function, which, by default, is ‘window’.
Use foo.apply(this, …) or foo.call(this, …) to invoke foo such that ‘this’ in foo refers to the ‘this’ that called foo.
The convention I use (to avoid .apply and .call) is:
BTW, ‘Me’ is the keyword for ‘this’ in VB.NET.