Javascript 调用回调的事件调用回调

Javascript 调用回调的事件调用回调,javascript,module,callback,dom-events,Javascript,Module,Callback,Dom Events,我正在实现模块模式,我想公开一个事件函数。 代码如下: var module = (function(){ var self = {}, cb = $(":checkbox"); cb.on("change",/* self.OnChange */ ); // ??? return self; }()); module.OnChange(callbackFunction); function callbackFunction(event){

我正在实现模块模式,我想公开一个事件函数。
代码如下:

var module = (function(){
    var self = {},
        cb = $(":checkbox");

    cb.on("change",/* self.OnChange */ ); // ???

    return self;
}());

module.OnChange(callbackFunction);

function callbackFunction(event){
    //do staff
}
那么,关于如何访问
OnChange
“callbackFunction”有什么想法吗?

还是用模块模式来实现这一点的更好方法?

我认为您在制造一些混乱

var module = (function(){
    // here this points to the window object
    var self = this,
        cb = $(":checkbox");

    cb.on("change",/* self.OnChange */ ); // ???
    // here you are returning the window object
    return self;
}());
//since the function was instantaneous, here you are calling onChange() on the window object
module.OnChange(callbackFunction);

function callbackFunction(event){
    //do staff
}

你到底想做什么?

我经常在我的模块/插件/库中使用这种模式:

var borrow = function( obj, funcName, funcArg ){
  return function(){
    /// convert arguments to array
    var args = Array.prototype.slice.call( arguments );
    /// add in our fixed arg 'change'
    args.unshift( funcArg );
    /// execute the function
    return obj[funcName].apply( obj, args );
  }
}

self.change = borrow( cb, 'on', 'change' );
这意味着您可以在构造函数之外调用:

module.change( callbackFunction );
这基本上具有直接借用jQuery函数的效果,但是使用您选择的特定元素包装它。上述内容将直接将事件侦听器传递到复选框,就像您直接键入以下内容一样:

cb.on( 'change', callbackFunction );
您可以改进上述方法以接受多个固定参数,如下所示:

var borrow = function( obj, funcName ){
  /// convert arguments to array
  var args1 = Array.prototype.slice.call( arguments );
  /// remove the first two args (obj & funcName) 
  /// which means we now have an array of left over arguments
  /// we'll treat these as 'fixed' and always passed to the 
  /// 'borrowed' function.
  args1.shift(); args1.shift();
  /// return a closure containing our 'borrowed' function
  return function(){
    /// convert arguments to array
    var args2 = Array.prototype.slice.call( arguments );
    /// create a new array combined from the fixed args and the variable ones
    var args = args1.concat( args2 );
    /// execute the function
    return obj[funcName].apply( obj, args );
  }
}
进一步的改进(消除轮班)如下:

var borrow = function( obj, funcName ){
  /// convert arguments to array and remove first two arguments
  var args1 = Array.prototype.slice.call( arguments, 2 );
  /// return a closure containing our 'borrowed' function
  return function(){
    /// convert arguments to array
    var args2 = Array.prototype.slice.call( arguments );
    /// create a new array combined from the fixed args and the variable ones
    var args = args1.concat( args2 );
    /// execute the function
    return obj[funcName].apply( obj, args );
  }
}
@丹尼尔:没问题。。。非常恼人的是,
参数
对象在转换之前不支持普通的数组方法,否则代码会非常整洁。哦,顺便说一句,我添加了一个更好的版本,不再需要数组shift()。