简体   繁体   English

什么是javascript模式的名称,其中包含一个赋值函数返回值的变量?

[英]Whats the name for a javascript pattern with a variable that is assigned the return value of a function?

What the proper name of this Javascript pattern where a variable is assigned to value of a return function? 这个Javascript模式的正确名称是什么,其中变量被赋值给返回函数的值?

// array with a ton of random values. 
var once = (function(){

        var i = 10000, arr = [];

        while(i){
            arr.push( Math.random() * i );
            i--;
        }
        arr = arr.toString(); 

        return (function(){
            return arr;
        }());

}());

Edit - a better example: 编辑 - 一个更好的例子:

 var once = (function(){

    // Only run a really expensive operation once...
    var i = 10000, arr = [], x;

    while(i){
        arr.push( Math.random() * i );
        i--;
    }
    arr = arr.toString(); 
    x = parseFloat(arr.toString());

    // then return the result of another function
    return function(){
        return x * (Math.random() * 10);
    };

}());

$(window).resize(function(){
    console.info(once());
});

That is a self-invoking anonymous function. 这是一个自我调用的匿名函数。

It is useful when you want to do some work, but keep the variables out of scope. 当您想要做一些工作时,它很有用,但要将变量保持在范围之外。 This can be particularly important if you are worried about memory leaks - the variables used in the anonymous function go immediately out of scope, and can be cleaned up by the garbage collector. 如果您担心内存泄漏,这一点尤其重要 - 匿名函数中使用的变量会立即超出范围,并且可以被垃圾收集器清理。 Of course, if you return a closure, the opposite happens - those variables remain for the life of the closure. 当然,如果你返回一个闭包,则会发生相反的情况 - 这些变量会在闭包的生命周期内保留。

Edit: Your second example is a closure. 编辑:你的第二个例子是一个闭包。 Your closure looks a lot like your self invoking anonymous function, but, as I mentioned above, it behaves quite differently. 你的闭包看起来很像你自己调用的匿名函数,但是,正如我上面提到的,它的行为完全不同。

I'd go on a bit more about closures, but I'm answering from my phone, and that kind of an answer requires all ten fingers... maybe I'll come back to this later. 我会更多关于闭合,但是我正在通过手机回答,这种答案需要十个手指......也许我稍后会再回来看看。

I believe you're looking for memoization . 我相信你正在寻找备忘录

In computing, memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously processed inputs. 在计算中,memoization是一种优化技术,主要用于通过函数调用来避免重复计算先前处理的输入的结果来加速计算机程序。

... ...

A memoized function "remembers" the results corresponding to some set of specific inputs. 记忆功能“记住”与某些特定输入相对应的结果。 Subsequent calls with remembered inputs return the remembered result rather than recalculating it, thus eliminating the primary cost of a call with given parameters from all but the first call made to the function with those parameters. 使用记忆输入的后续调用将返回记住的结果而不是重新计算结果,从而消除了除了使用这些参数对函数进行的第一次调用之外的所有参数的调用的主要成本。

Is it "currying"? 是“currying”吗?

A function of N arguments that is considered as a function of one argument which returns another function of N-1 arguments. N个参数的函数,被认为是一个参数的函数,它返回N-1个参数的另一个函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM