Using jsPlumb to bind, I'm passing a method as a variable to be used as a callback. When its called, the "this" is not the object the method is a member of. How do I go about getting access to the method's object instance so I can get access to it variables and other member functions?
I have no control over the callback call method, its a separate library. All I do is call the binding from my object init method. I would have expected this
inside my _connection
method to have been its object.
jsPlumb.bind('connection', this._connection);
The value of this
for any function
is determined when it's called. The function
really has no other ties to the "object" than whether or not it was called as object.method()
or method.call(object)
.
So, to pass a function
with a fixed this
value, it'll need to be bound:
jsPlumb.bind('connection', this._connection.bind(this));
jQuery also includes a wrapper and compatibility fill for this with jQuery.proxy()
:
jsPlumb.bind('connection', $.proxy(this._connection, this));