admin管理员组文章数量:1278979
I have created a simple javascript class using the 'function' technique. In the class I have a websocket listener which triggers a function when a certain message is received. I could easily add an external callback to it, as follows
function MyClass() {
self = this; // to access main object from methods
// websocket definition
function websocketMessageInterpreter(message){
if(message == "Hello!") onHelloMessageBase();
}
function onHelloMessageBase(param){
// call user defined callback
self.userDefinedCallback(param);
}
this.userDefinedCallback= function(param){}; //default empty callback
}
Externally I use this as
var myObject = new MyClass();
myObject.userDefinedCallback = function(){alert("Someone said hello!");};
Is there a model where I could do something like this?
myObject.userDefinedCallback += function1;
myObject.userDefinedCallback += function2;
and maybe later
myObject.userDefinedCallback -= function1;
I have created a simple javascript class using the 'function' technique. In the class I have a websocket listener which triggers a function when a certain message is received. I could easily add an external callback to it, as follows
function MyClass() {
self = this; // to access main object from methods
// websocket definition
function websocketMessageInterpreter(message){
if(message == "Hello!") onHelloMessageBase();
}
function onHelloMessageBase(param){
// call user defined callback
self.userDefinedCallback(param);
}
this.userDefinedCallback= function(param){}; //default empty callback
}
Externally I use this as
var myObject = new MyClass();
myObject.userDefinedCallback = function(){alert("Someone said hello!");};
Is there a model where I could do something like this?
myObject.userDefinedCallback += function1;
myObject.userDefinedCallback += function2;
and maybe later
myObject.userDefinedCallback -= function1;
Share
Improve this question
asked Jan 16, 2013 at 12:34
malbermalber
1,0734 gold badges16 silver badges24 bronze badges
1
- you could write nested functions into the callback – Johnny000 Commented Jan 16, 2013 at 12:39
1 Answer
Reset to default 11The usual way to do this is to have an array of callbacks, and methods like addCallback(callback)
and removeCallback(callback)
to let users of the API add and remove callbacks.
addCallback
is basically:
function addCallback(callback) {
if (this.callbacks.indexOf(callback) < 0) {
this.callbacks.push(callback);
}
}
removeCallback
is basically:
function removeCallback(callback) {
var index;
if ((index = this.callbacks.indexOf(callback)) >= 0) {
this.callbacks.splice(index, 1);
}
}
If you need to support really old browsers like IE6 which may not have Array#indexOf
, since you're using jQuery, you can use jQuery.inArray
instead.
本文标签: jqueryHow to add multiple callbacks to a javascript methodStack Overflow
版权声明:本文标题:jquery - How to add multiple callbacks to a javascript method? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741251984a2365964.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论