试图理解闭包。(Trying to understand closures. Could somebody walk me through this code?)

编程入门 行业动态 更新时间:2024-10-27 22:26:53
试图理解闭包。(Trying to understand closures. Could somebody walk me through this code?)

这里有一点从这个reddit post取得的javascript:

function Stream() { var data = [], listeners = []; function push( new_data ) { var result = data.push( new_data ); callListeners( new_data, result ); return result; } function addListener( listener ) { return listeners.push( listener ); } function callListeners( ) { var length = listeners.length, result = [], action = null; while ( length-- ) { action = listeners[ length ]; result.push( action.apply( null, arguments) ); } return result; } return { push : push, addListener: addListener } } var foo = Stream(); foo.addListener( function( new_data ) { alert( "added: " + new_data ); }); foo.push( "Hello World!" );

在阅读本教程后 ,我认为我对闭包有一个微妙的把握,但我无法弄清楚这段代码是如何工作的。 当我试图解析它时,我基本上被卡在第6行: var result = data.push( new_data ); 。

data似乎只是在这一点上是一个数组 data.push( foo )没有意义。 无论如何,它不会无限递增? (罢工 - 不知道是否有数组的本地push方法)非常接下来的一行callListener被两个参数调用,但在该函数下面没有。

如果某人有几分钟的时间,你能抓住我的手,并像我那个无知的玩偶一样走过这段代码吗? 现在,我甚至不确定我是否理解目的地。

Here's a bit of javascript taken from this reddit post:

function Stream() { var data = [], listeners = []; function push( new_data ) { var result = data.push( new_data ); callListeners( new_data, result ); return result; } function addListener( listener ) { return listeners.push( listener ); } function callListeners( ) { var length = listeners.length, result = [], action = null; while ( length-- ) { action = listeners[ length ]; result.push( action.apply( null, arguments) ); } return result; } return { push : push, addListener: addListener } } var foo = Stream(); foo.addListener( function( new_data ) { alert( "added: " + new_data ); }); foo.push( "Hello World!" );

I think I have a tenuous grasp on Closures after reading this tutorial, but I just can't figure out how this code works. When I try to parse it in my head, I basically get stuck at line 6: var result = data.push( new_data );.

It seems with data simply being an array at that point data.push( foo ) doesn't make sense. And wouldn't it recurse infinitely anyway? (strike that -- didn't know there was a native push method for arrays) Very next line callListener is called with two parameters, but below the function has none.

If someone's got a few minutes, could you grab my hand and walk me through this code like the ignorant dolt I am? Right now, I'm not even sure I understand the destination.

最满意答案

数组对象,它们有一个push()方法。 没什么不寻常的。

callListeners()函数没有声明任何命名参数,但是JavaScript允许使用比声明的参数更多的参数来调用函数,并且完整的参数列表可用作特殊名称arguments 。 callListeners()在action.apply()调用中使用arguments来调用具有callListeners()本身的相同参数列表的action函数。 callListeners()的目的是用一些参数调用它,并用这些参数调用listeners数组中的所有函数。

不过,这些东西都不是真正与封闭的使用相关的。 封闭起作用的地方在于Stream()返回的对象有两个方法push()和addListener() ,它们可以“看到”相同的data和listeners数组,即使这些数组没有存储在对象中该方法被调用。 两次调用Stream()将返回两个对象,其方法可以看到不同的 data和listeners数组。

Arrays are objects, and they have a push() method. Nothing unusual there.

The callListeners() function doesn't declare any named parameters, but JavaScript allows functions to be called with more parameters than they're declared to take, and the full list of arguments is available as the special name arguments. callListeners() uses arguments in an action.apply() call, to invoke the action function with the same list of arguments that callListeners() itself was given. The purpose of callListeners() is that you call it with some arguments, and it calls all the functions in the listeners array with those arguments.

Neither of those things is really related to the use of closures, though. The place where closures come into play is that the object returned by Stream() has two methods, push() and addListener(), that can "see" the same data and listeners arrays even though those arrays aren't stored in the object that the methods are called on. Two calls to Stream() will return two objects whose methods see different data and listeners arrays.

更多推荐

本文发布于:2023-07-16 12:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128516.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:closures   understand   code   walk

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!