i was wondering how to implement a callback into this piece of code
MyClass.myMethod("sth.", myCallback);
function myCallback() { // do sth };
var MyClass = {
myMethod : function(params, callback) {
// do some stuff
FB.method: 'sth.',
'perms': 'sth.'
'display': 'iframe'
},
function(response) {
if (response.perms != null) {
// How to pass response to callback ?
} else {
// How to pass response to callback ?
}
});
}
}
three ways to achieve "// How to pass response to callback ?" :
callback(response, otherArg1, otherArg2);
callback.call(this, response, otherArg1, otherArg2);
callback.apply(this, [response, otherArg1, otherArg2]);
1 is the simplest, 2 is in case you want to control the 'this
' variable's value inside your callbackfunction, and 3 is similar to 2, but you can pass a variable number of arguments to callback
.
here is a decent reference: http://odetocode.com/Blogs/scott/archive/2007/07/05/function-apply-and-function-call-in-javascript.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With