jQuery:我如何知道我正在使用多个回调函数调用正确的回调函数?

问题描述:

我有这样jQuery:我如何知道我正在使用多个回调函数调用正确的回调函数?

 

$.fn.myFunction = function(options, special, callback) { 
    var settings = $.extend({ 
     opacity  : '', 
     margin : '' 
    }, options); 

    //while executing this main function, some cool things happen 
    if (special && typeof special === "function" && things_are_running) { 
     special(); 
    } 

    //when finished executing the main function, there is one more thing 
    if (callback && typeof callback === "function" && things_stopped) { 
     callback(); 
    } 
} 

一个功能我真的很喜欢这个

$('.selector').myFunction(options, function() { /* this is the question about*/ }); 

一些有趣的东西,我怎么能知道我打电话的special()callback()功能只考虑一个回调给出?

我应该做这样的事情?

$('.selector').myFunction(options, function() {}, null); 

或这?

$('.selector').myFunction(options, null, function() {}); 

是的,但你的第一个例子中,你也可以使用

$('.selector').myFunction(options, function() {}); 

的功能将被分配到特别,

if (callback && typeof callback === "function" && things_stopped) { 

回调将被评估为假。

+0

谢谢巴里,我会尽快测试,并让你知道。 – thednp 2014-10-05 14:36:02