简体   繁体   English

在javascript类中设置回调函数

[英]Set a callback function inside a javascript class

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? 那么我如何将itWorked()传递给类? And how would I call that itWorked(answer) function within the class as well as passing a variable to if? 我如何在类中调用itWorked(answer)函数以及将变量传递给if?

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. 范围可以是任何对象,如果您愿意,也可以是空的对象。

By the way, I really like your use of private variables in this example, great work with the javascript. 顺便说一句,我真的很喜欢你在这个例子中使用私有变量,使用javascript工作很棒。 Here is a good way to write your javascript object to help with the initialization and readability 这是编写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");

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 );

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

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