Hello,
I have a generic question about javascript specification or implementation of functions pointer (delegates?) which are points to object methods.
Please, read the following code snippet. Here we have an object with a method using 'this' to access an object field. When we call this method as usual (o.method();
), returns value of the specified field of the object. But when we create pointer to this method (callback) and invoke it, returns an undefined value, because 'this' inside method's scope now is global object.
var o = {
field : 'value',
method : function() {
return this.field;
}
};
o.method(); // returns 'value'
var callback = o.method;
callback(); // returns 'undefined' cause 'this' is global object
So, where is my 'this'?