如何重构这个Javascript匿名函数? [英] How to refactor this Javascript anonymous function?

查看:116
本文介绍了如何重构这个Javascript匿名函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的代码中有这个匿名函数,它是jQuery的Ajax对象参数的一部分,它使用调用它的函数中的一些变量。

We have this anonymous function in our code, which is part of the jQuery's Ajax object parameters and which uses some variables from the function it is called from.

this.invoke = function(method, data, callback, error, bare) {
      $.ajax({
        success: function(res) {
            if (!callback) return;

            var result = "";
            if (res != null && res.length != 0)
                var result = JSON2.parse(res);

            if (bare)
            { callback(result); return; }

            for (var property in result) {
                callback(result[property]);
                break;
            }
        }
   });
}

我省略了额外的代码,但你明白了。代码工作得很好,但它在IE中每次调用时泄漏4 Kbs,所以我想重构它以将匿名函数转换为命名函数,如this.onSuccess = function(res){..}。

I have omitted the extra code, but you get the idea. The code works perfectly fine, but it leaks 4 Kbs on each call in IE, so I want to refactor it to turn the anonymous function into a named one, like this.onSuccess = function(res) { .. }.

问题是这个函数使用了this.invoke(..)中的变量,所以我不能把它带到它的体外。如何正确重构此代码,以便使用匿名函数和父函数变量?

The problem is that this function uses variables from this.invoke(..), so I cannot just take it outside of its body. How do I correctly refactor this code, so that it does not use anonymous functions and parent function variables?

更新。我正在考虑创建一个单独的对象,用相同的参数初始化它,并将其onSuccess函数作为jQuery的Ajax对象的参数传递。虽然我怀疑它仍然会泄漏内存。

Update. I am thinking of creating a separate object, initializing it with the same parameters, and pass its onSuccess function as a parameter for jQuery's Ajax object. Although I suspect that it will still leak memory.

更新2。我发现一些链接表明实际泄漏可能是由于jQuery的。
简单的jQuery Ajax调用在Internet Explorer中泄漏内存
涉及jQuery Ajax请求的内存泄漏

Update 2. I have found a few links suggesting that the actual leak might be caused by jQuery. Simple jQuery Ajax call leaks memory in Internet Explorer Memory leak involving jQuery Ajax requests

找到一种重构方法仍然很好。

Still it was good to find a way to refactor this.

更新3。我会等对于更通用的解决方案,在接受答案之前。

Update 3. I will wait for a more generic solution, before accepting an answer.

推荐答案

您可以在ajax请求中添加额外的参数,可以在成功回调:

You can add extra params to the ajax request that can be accessed in the success callback:

this.invoke = function(method, data, callback, error, bare) {
    $.ajax({
        success: onSuccess,
        invokedata: {
         callback: callback,
         bare: bare
        }
    });
};

var onSuccess = function(res) {
    var callback = this.invokedata.callback,
        bare = this.invokedata.bare;
    if (!callback) return;

    var result = "";
    if (res != null && res.length != 0)
        var result = JSON2.parse(res);

    if (bare){
        callback(result); 
        return;
    }

    for (var property in result) {
        callback(result[property]);
        break;
    }
}

这篇关于如何重构这个Javascript匿名函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆