I am trying to do the following:
function main(callback) {
$.ajax('server-side', function() {
this.callback.call("hello");
}.bind({ callback: callback });
}
main(function(response) {
alert(response);
});
Response is undefined
, I would expect it to be "hello"
. Any ideas why?
call
first argument should be a reference to "this". Being "this" the context where you want to execute your function.
Call function Mozila MDN
You wrote :
function main(callback) {
$.ajax('server-side', function() {
this.callback.call("hello");
}.bind({ callback: callback });
}
main(function(response) {
print response;
});
print doesnt exists in javascript.
then you wrote this.callback.call , which is wrong
you should write
callback.call(this,"hello") ,
just check the call function signature.
this
because of the bind() and bindding an object containing the callback. Need to use bind() because by the time the callback fires from the ajax event the passed in parameter callback
will be out of scope - Justin 2012-04-04 06:47