简体   繁体   English

创建一个Javascript回调函数?

[英]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 . 1是最简单的,2是你想要在你的回调函数中控制' this '变量的值,而3类似于2,但你可以传递可变数量的参数来callback

here is a decent reference: http://odetocode.com/Blogs/scott/archive/2007/07/05/function-apply-and-function-call-in-javascript.aspx 这是一个不错的参考: 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) . 在这种情况下,您只需要进行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. 我想你可以简单地在那里调用callback(response.perms) 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 :-) 而已 :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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