0
我得有一個方法,我可以添加回調的原型:(請忽略data_arg =真正的,因爲它是不相關的其他部分)奇怪的遞歸無限循環,我不能追查
/*
* Add a callback function that is invoked on every element submitted and must return a data object.
* May be used as well for transmitting static data.
*
* The callback function is supposed to expect a jQuery element as single parameter
* and must return a data object (for additional data to be sent along with the one already given upon initialization).
* Adding multiple callback functions results in those functions being invoked in the same order as they were added.
*
* 1) Therefore subsequent assignments to the same key in the data array override those that were performed by a previous callback function.
* 2) If data_arg = true is given, the data returned by the callback function that was previously called is given to the new_callback as 2nd argument, so it can be manipulated.
* However, even if it isn't, the unchanged data must be returned anyway to have any effect.
*/
this.add_data_callback = function(new_callback, data_arg) {
if(this.data_callback) {
old_callback = this.data_callback;
if(!data_arg) {
//alert('add it');
//alert(old_callback);
//alert(new_callback);
this.data_callback = function(element) {
//alert('do it');
//alert(old_callback);
//alert(new_callback);
return jQuery.extend(old_callback(element), new_callback(element));
};
}
else {
this.data_callback = function(element, data) {
return new_callback(element, old_callback(element));
};
}
}
else {
//alert('add the first');
//alert(new_callback);
this.data_callback = new_callback;
}
};
在我的具體情況下,我添加了三個回調函數。如果最後爲元素調用this.data_callback(),則會導致整個事物循環無限。 在我試圖跟蹤bug了,上面的警報(是的,我知道有是爲調試工具,但它是方式更舒服像)提供以下洞察問題:
- 的匿名函數/閉包(包含jQuery.extend()的函數)被調用。 new_callback包含第三個回調函數。 old_callback包含另一個...
- 被調用的匿名函數。 new_callback包含第二個回調函數。 old_callback應該包含第一個回調函數,但實際上它是另一個...
- 匿名回調被調用。 new_callback再次包含第二個回調函數。 old_callback包含匿名函數
- ...
現在,我錯過?這是一些奇怪的關閉魔法還是我顯然無法看到的一些明顯的錯誤?
在此先感謝!
通過使用'this'關鍵字判斷,發佈的代碼看起來像是類的一部分,而old_callback可能是該類的私有屬性。 – 2010-03-29 13:52:46
Awwwww。 @安迪:事實並非如此。 – balu 2010-03-29 13:55:04
@codethief:繼續;-) @Pointy:+1。 – 2010-03-29 14:27:04