关闭解释 [英] Closures Explained

查看:51
本文介绍了关闭解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经改写了一篇简短的文章来解释JavaScript中的闭包。

它是

at:

http://www.martinrinehart.com/articl...-closures.html


非常感谢PointedEars和Jorge帮助我更接近

真相。

I''ve rewritten a short article explaining closures in JavaScript.
It''s
at:

http://www.martinrinehart.com/articl...-closures.html

A big Thank You to PointedEars and Jorge for helping me get closer to
the truth.

推荐答案

10月10日,11:38 * am,MartinRineh ... @ gmail.com写道:
On Oct 10, 11:38*am, MartinRineh...@gmail.com wrote:

我已经重写了一篇用JavaScript解释闭包的简短文章。

它是

* at:

http://www.martinrinehart.com/articl...-closures.html



它开始可疑:


我有最好的书(Flanagan,Resig,Crockford)"


F. lanagan已被证明是无能为力的,Resig'的书籍属于

喜剧架(或篝火)。


并且没有多少那里。这是一篇非常优秀的文章:

http ://www.jibbering.com/faq/faq_notes/closures.html


Ma ************ @ gmail.com 写道:
Ma************@gmail.com writes:

我已经重写了一篇用JavaScript解释闭包的简短文章。

它是

at:

http://www.martinrinehart.com/articl...-closures.html


非常感谢PointedEars和Jorge帮助我更接近

真相。
I''ve rewritten a short article explaining closures in JavaScript.
It''s
at:

http://www.martinrinehart.com/articl...-closures.html

A big Thank You to PointedEars and Jorge for helping me get closer to
the truth.



评论:


在示例中:

function outer(){

// args这里

//这里的代码

函数inner(){

//更多args和代码

}

} //外部结束()

args here评论似乎有点误导。括号之间的参数是
。也许使用:

函数外(/ *参数在这里*){

代替。


句子封闭是一组操纵数据和代码的数据和代码。仍然无法与

对象的描述区分开来。


闭包是一段代码以及对值的绑定

该代码的自由变量。


自由变量是代码中出现的变量,但是没有声明

那里。


下一句话,在JavaScript中它是一个函数,其数据是'

不可用,除了函数。更接近,但也错了。

数据,甚至绑定,也可用于其他代码。


示例:


函数对(a,b){

return {setFst:function(x){a = x;},

setSnd:function( x){b = x;},

sum:function(){return a + b; } bb b = b $

//可通过例如pair.setFst获得:

alert(s());

p.setFst(38);

alert(s());


所以,你所描述的是什么?关闭"?;不对。这是一个闭包的一个用途:使用共享的私有变量来创建函数。

似乎这个闭包的使用是你页面的焦点,但它应该

真的被命名为Javascript中的私有变量,使用闭包相反。


闭包不仅仅是这个,例如:


函数咖喱(f){

返回功能(a){

返回功能(b){

返回f(a,b);

}

}

}

函数和(a,b){return a + b;};

var csum = curry(sum );

var inc = csum(1);

var addFive = csum(5);

alert([inc(41), addFive(37)]);

或者它可以用来以更简单的方式表达复杂的东西:


函数图(arr,f){

var res = [];

for(var i = 0; i< arr.length; i ++){

res [i] = f(arr [i]);

}

返回res;

}


函数交叉(arr1,arr2){

返回地图(arr1,函数(x1){

返回地图(arr2,函数(x2){return [x1,x2] ;});

});

}


var pair_matrix = cross([1,2,3],[ 4,5,6]);

// pair_matrix ==

// [[[1,4],[1,5],[1] ,6]],

// [[2,4],[2,5],[2,6]],

// [[3,4 ],[3,5],[3,6]]]


(尝试使用嵌套循环缩短它)


And美化它:


提醒(地图(pair_matrix,功能(a1){

返回地图(a1,功能(a2){

return"(" + a2 [0] +, + a2 [1] +")" ;;

})。join(",");

})。join(" \\\
" ;));


直截了当地说:Closures允许你编写功能程序。

如果我们只有尾调用,那就完美了: )


/ L

-

Lasse Reichstein Nielsen

DHTML死亡颜色:<网址:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低精神神圣。''

Comments:

In the example:
function outer() {
// args here
// code here
function inner() {
// more args and code
}
} // end of outer()
the "args here" comment seems slightly misleading. Arguments goes
between the parentheses. Maybe use:
function outer(/* arguments here *) {
instead.

The sentence "A closure is a bundle of data and code that manipulates
that data" is still indistinguishable from the description of an
Object.

A closure is a piece of code together with a binding of values to the
free variables of that code.

A free variable is one that occur in the code, but is not declared
there.

The next sentence, "In JavaScript it''s a function with data that''s
unavailable, except to the function." is closer, but also wrong.
The data, and even the bindings, might be available to other code too.

Example:

function pair(a,b) {
return {setFst : function(x){a=x;},
setSnd : function(x){b=x;},
sum : function() { return a+b; }};
}

var p = pair(2,4);
var s = p.sum; // s holds a clousre, but its data and bindings
// are available through, e.g., pair.setFst:
alert(s());
p.setFst(38);
alert(s());

So, what you describe as "What is a Closure?" isn''t correct. It is
one use of a closure: to create functions with shared private variables.
It seems this use of closures is the focus of your page, but it should
really be named "private variables in Javascript, using closures" instead.

Closures are MUCH more than just that, e.g.:

function curry(f) {
return function(a) {
return function (b) {
return f(a,b);
}
}
}
function sum(a,b) { return a+b;};
var csum = curry(sum);
var inc = csum(1);
var addFive = csum(5);
alert([inc(41),addFive(37)]);
or it can be used to express something complicated in a simpler way:

function map(arr,f) {
var res = [];
for(var i = 0; i < arr.length; i++) {
res[i] = f(arr[i]);
}
return res;
}

function cross(arr1,arr2) {
return map(arr1, function(x1) {
return map(arr2, function(x2) { return [x1,x2]; });
});
}

var pair_matrix = cross([1,2,3],[4,5,6]);
// pair_matrix ==
// [[[1,4],[1,5],[1,6]],
// [[2,4],[2,5],[2,6]],
// [[3,4],[3,5],[3,6]]]

(try doing it shorter using nested loops :)

And to prettify it:

alert(map(pair_matrix, function(a1) {
return map(a1, function(a2) {
return "(" + a2[0] + "," + a2[1] +")";
}).join(",");
}).join("\n"));

To put it bluntly: Closures allow you to write functional programs.
If only we had tail-calls, it would be perfect :)

/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''




David Mark写道:


David Mark wrote:

"我有最好的书(Flanagan,Resig,Crockford)


Flanagan已被证明是无能为力的,而Resig的书籍属于

喜剧架(或篝火)。


并没有那么多。这是一篇非常优秀的文章:

http ://www.jibbering.com/faq/faq_notes/closures.html



Flanagan在FAQ中列为最佳JavaScript书。


这篇简短的文章很好。但是,它的时间要长10倍。我的目标

是为了给一些可怜的JavaScripter提供基本的想法,所以他/她

可以继续编写代码。

Flanagan is listed in the FAQ as the best JavaScript book.

The jibbering article is good. However, it''s 10 times longer. My goal
was to give some poor JavaScripter wannabe the basic idea so s/he
could get on with writing code.

这篇关于关闭解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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