奇怪的递归无限循环我无法追踪

发布于 2024-08-26 13:42:36 字数 2295 浏览 9 评论 0原文

我有一个原型,有一个可以添加回调的方法:(

/*
 * 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;
    }
};

请忽略 data_arg = true 的 else 部分,因为它不相关。)

在我的具体情况下,我添加了三个回调函数。然而,如果最终为一个元素调用 this.data_callback() ,则会导致整个事件无限循环。 在我尝试追踪错误时,上面的警报(是的,我知道有调试工具,但这样更舒服)提供了对该问题的以下见解:

  1. 匿名函数/闭包(包含 jQuery.extend()) 被调用。 new_callback 包含第三个回调函数。 old_callback 包含另一个...
  2. 被调用的匿名函数。 new_callback 包含第二个回调函数。 old_callback 应该包含第一个回调函数,但实际上它是另一个......
  3. 被调用的匿名回调。 new_callback 再次包含第二个回调函数。 old_callback 包含匿名函数
  4. ...

现在,我错过了什么?这是一些奇怪的闭合魔法还是只是一些我显然看不到的明显错误?

提前致谢!

I've got a prototype having a method I can add callbacks with:

/*
 * 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;
    }
};

(Please ignore the else part for data_arg = true as it's not relevant.)

In my concrete case I'm adding three callback functions. If this.data_callback() is finally invoked for an element, however, it results in the whole thing looping infinitely.
In my attempt to track the bug down, the alerts above (yeah, I know that there're debugging tools for that but it was way more comfortable like that) delivered the following insight into the issue:

  1. The anonymous function / closure (the one that contains jQuery.extend()) is called.
    new_callback contains the 3rd callback function.
    old_callback contains another...
  2. anonymous function which is called.
    new_callback contains the 2nd callback function.
    old_callback should contain the 1st callback function, but actually it's yet another...
  3. anonymous callback which is called.
    new_callback contains the 2nd callback function again.
    old_callback contains the anonymous function
  4. ...

Now, what do I miss? Is it some strange closure magic or just some obvious bug I'm apparently unable to see?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病毒体 2024-09-02 13:42:36

为什么 old_callback 不使用 var 定义?

var old_callback = this.data_callback;

事实上,它是一个全局变量。也许它在其他地方声明过,但对我来说仍然看起来很可疑。

Why isn't old_callback defined with var?

var old_callback = this.data_callback;

As it is, it's a global variable. Maybe it's declared elsewhere but it still looks suspicious to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文