Yes, when you get a method as a function reference, you disconnect it from the object. A function only works as a method when you call it in the context of an object, which is usually done with the .
operator, e.g. obj.method()
.
If you call a function without the context of an object, it's called with the global scope as context, i.e. the window
object. Example:
var obj = {
name: "obj",
method: function() { alert(this.name); }
};
obj.method(); // shows the name "obj"
var m = obj.method;
m(); // shows the name of window
m.call(obj); // shows the name "obj"
var obj2 = {
name: "obj2"
};
m.call(obj2); // shows the name "obj2" (using the method from obj)
If you want to make it work as a method, you have to call it with the object as context:
var obj = $('#my-div');
var fadeOutFn = obj.fadeOut;
fadeOutFn.call(obj);
You can use the proxy
method to make a function that calls the function with the correct context:
var obj = $('#my-div');
var fadeOutFn = $.proxy(obj.fadeOut, obj);
fadeOutFn();