2014-10-05 99 views
0

我有這樣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() {}); 

回答

1

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

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

的功能將被分配到特別,

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

回調將被評估爲假。

+0

謝謝巴里,我會盡快測試,並讓你知道。 – thednp 2014-10-05 14:36:02

相關問題