简体   繁体   中英

Create a Javascript Callback function?

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 ?" :

  1. callback(response, otherArg1, otherArg2);
  2. callback.call(this, response, otherArg1, otherArg2);
  3. 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

All you have to do is call the callback function the normal way. In this case you would just do callback(response) .

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 ?
            // Easy as:
            callback(response);
        } else {
            // How to pass response to callback ?
            // Again:
            callback(response);
        }
  });
}

}

只需调用传入的函数。

callback(response)

I think you can simply call callback(response.perms) there. You could also register it as a

member of your class:

  MyClass.cb = callback;

and later call it:

 MyClass.cb(response.perms)

You're close... just use the callback. In this instance, you can form a closure.

var MyClass = {

myMethod : function(params, callback) {

    // do some stuff

    FB.method: 'sth.',
       'perms': 'sth.'
       'display': 'iframe'
      },
      function(response) {

            if (response.perms != null) {
                callback(response);
            } else {
                // response is null, but you can pass it the same as you did above - if you want to.  Probably better to have a generic failure handler
                ajaxFailHandler();
            }
      });
}
callback.call(null, response);
MyClass.myMethod("sth.", myCallback);
var 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) {
                callback();
            } else {
                callback();
            }
      });
}

}

You've got a reference to a function now. Just invoke it:

callback(response.perms);
var callback = function() {

};

Thats it :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM