在 javascript 类中设置回调函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8286300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:56:14  来源:igfitidea点击:

Set a callback function inside a javascript class

javascriptclasscallback

提问by ed209

excuse the pseudo code, my actual file is much larger:/

请原谅伪代码,我的实际文件要大得多:/

I want to call a function (with parameters) from inside a class. However, that function should be passed to the class as a variable.

我想从类内部调用一个函数(带参数)。但是,该函数应该作为变量传递给类。

someObject = {
    itWorked:function(answer){
       alert(answer);
    },

    plugins:{
        somePlugin:function(){

            var callback;
            this.doSomething = doSomething;

            function setCallback(c){
                callback = c;
            }

            function doSomething(){
                 var answer = "hello";
                 [callback](answer); // how do I call this?
            }

        }
    },

    widgets:{
        something:function(){
            var doIt = new someObject();
            doIt.setCallback(someObject.itWorked()); // how do I send this?
            doIt.doSomething();
        }
    }
}

So how would I pass itWorked()to the class? And how would I call that itWorked(answer)function within the class as well as passing a variable to if?

那么我将如何传递itWorked()给班级?我将如何itWorked(answer)在类中调用该函数并将变量传递给 if?

采纳答案by JJJ

Remove the parentheses to pass the function as a variable.

删除括号以将函数作为变量传递。

doIt.setCallback( someObject.itWorked );

You can then use the callback as you would any other function.

然后,您可以像使用任何其他函数一样使用回调。

callback( answer );

回答by Zoidberg

You will need to change

你需要改变

setCallback = function (c) {callback = c;}

to

this.setCallback =  function (c) {callback = c;}

so the setCallback function will be public.

所以 setCallback 函数将是公开的。

If you also want to scope the callback, you can call it like this

如果您还想限定回调范围,可以这样调用

callback.call(scope, param1, param2);

If you don't know how many parameters, you can call it like this

如果不知道有多少参数,可以这样调用

callback.apply(scope, parameters);

Scope could be any object, even an empty one {} if you want.

Scope 可以是任何对象,如果需要,甚至可以是空的 {}。

By the way, I really like your use of private variables in this example, great work with the javascript. Here is a good way to write your javascript object to help with the initialization and readability

顺便说一句,我真的很喜欢你在这个例子中使用私有变量,很好地使用了 javascript。这是编写 javascript 对象以帮助初始化和可读性的好方法

var mynamespace = {};

(function () {
   function MyObject(param1, param2) {
      this.initialize(param1, param2);
   }

   MyObject.prototype = {
      initialize: function (param1, param2) {
          var privateScope = {
              param1: param1,
              param2: param2,
              callback: null
          };

          this.setCallback = function (c) {
              privateScope.callback = c;
          }

          this.doSomething = function () {
              if (privateScope.callback) {
                  privateScope.callback.call();
              }
          }
      }
   }
   mynamespace.MyObject = MyObject;
}());

Then to use it

然后使用它

var obj = new mynamespace.MyObject("value1", "value2");