<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: ECMA-262-3 in detail. Chapter 6. Closures.</title>
	<atom:link href="http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/feed/" rel="self" type="application/rss+xml" />
	<link>http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/</link>
	<description>by Dmitry Soshnikov</description>
	<lastBuildDate>Sat, 04 Feb 2012 03:57:06 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Kostya</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/comment-page-1/#comment-7191</link>
		<dc:creator>Kostya</dc:creator>
		<pubDate>Wed, 24 Aug 2011 22:45:46 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=118#comment-7191</guid>
		<description>Now it became clear, thanks.</description>
		<content:encoded><![CDATA[<p>Now it became clear, thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmitry A. Soshnikov</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/comment-page-1/#comment-7189</link>
		<dc:creator>Dmitry A. Soshnikov</dc:creator>
		<pubDate>Wed, 24 Aug 2011 19:07:20 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=118#comment-7189</guid>
		<description>@&lt;b&gt;Kostya&lt;/b&gt;, thanks (I&#039;ll pass your wishes to ECMA ;))!

&lt;blockquote&gt;So, at what stage the creation of the function is performed. Code parsing?&lt;/blockquote&gt;

As mentioned in &lt;a href=&quot;http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/&quot; rel=&quot;nofollow&quot;&gt;ES3.Ch5&lt;/a&gt;, functions are divided into &lt;a href=&quot;http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#function-declaration&quot; rel=&quot;nofollow&quot;&gt;function declarations (FD)&lt;/a&gt; and &lt;a href=&quot;http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#function-expression&quot; rel=&quot;nofollow&quot;&gt;function expressions (FE)&lt;/a&gt;. The time when each of a function type is created differs, but still, it&#039;s not parsing stage.

At parsing stage only the parsing itself is applied, that is -- ES &lt;em&gt;concrete syntax&lt;/em&gt; is &lt;em&gt;transformed&lt;/em&gt; into the &lt;em&gt;abstract syntax&lt;/em&gt; tree (AST). At this stage, the parser already determined that it has functions in some positions (and even their types, FD, FE). But &lt;em&gt;runtime&lt;/em&gt; is not activated at this stage yet.

And only after that, when the code is &lt;em&gt;executed&lt;/em&gt;, then already the &lt;em&gt;interpretation&lt;/em&gt; of the parsed AST is started. Here already we &lt;em&gt;enter&lt;/em&gt; different &lt;a href=&quot;http://dmitrysoshnikov.com/ecmascript/chapter-1-execution-contexts/&quot; rel=&quot;nofollow&quot;&gt;execution contexts&lt;/a&gt;. And exactly &lt;em&gt;on entering the context&lt;/em&gt; stage (but &lt;em&gt;before&lt;/em&gt; its actual &lt;em&gt;code execution&lt;/em&gt;) the interpreter creates all FDs. And only when it starts to execute the code (the second stage of context handling) all FEs are created.

So the answer is -- no, not at parsing stage, but at runtime context handling stage, which is divided on two stages as well: the entering (FDs are created) and execution (FEs are created).</description>
		<content:encoded><![CDATA[<p>@<b>Kostya</b>, thanks (I&#8217;ll pass your wishes to ECMA <img src='http://dmitrysoshnikov.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> )!</p>
<blockquote><p>So, at what stage the creation of the function is performed. Code parsing?</p></blockquote>
<p>As mentioned in <a href="http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/" rel="nofollow">ES3.Ch5</a>, functions are divided into <a href="http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#function-declaration" rel="nofollow">function declarations (FD)</a> and <a href="http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#function-expression" rel="nofollow">function expressions (FE)</a>. The time when each of a function type is created differs, but still, it&#8217;s not parsing stage.</p>
<p>At parsing stage only the parsing itself is applied, that is &#8212; ES <em>concrete syntax</em> is <em>transformed</em> into the <em>abstract syntax</em> tree (AST). At this stage, the parser already determined that it has functions in some positions (and even their types, FD, FE). But <em>runtime</em> is not activated at this stage yet.</p>
<p>And only after that, when the code is <em>executed</em>, then already the <em>interpretation</em> of the parsed AST is started. Here already we <em>enter</em> different <a href="http://dmitrysoshnikov.com/ecmascript/chapter-1-execution-contexts/" rel="nofollow">execution contexts</a>. And exactly <em>on entering the context</em> stage (but <em>before</em> its actual <em>code execution</em>) the interpreter creates all FDs. And only when it starts to execute the code (the second stage of context handling) all FEs are created.</p>
<p>So the answer is &#8212; no, not at parsing stage, but at runtime context handling stage, which is divided on two stages as well: the entering (FDs are created) and execution (FEs are created).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kostya</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/comment-page-1/#comment-7182</link>
		<dc:creator>Kostya</dc:creator>
		<pubDate>Wed, 24 Aug 2011 09:14:59 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=118#comment-7182</guid>
		<description>Hi, Dmitry. I&#039;m really admire what you do. ECMA should have given you the share of the money for doing their work.

Anyways, I have a question. In all your articles you mention that the [[Scope]] variable is saved and does not change after creation. So, at what stage the creation of the function is performed. Code parsing?

Thanks.</description>
		<content:encoded><![CDATA[<p>Hi, Dmitry. I&#8217;m really admire what you do. ECMA should have given you the share of the money for doing their work.</p>
<p>Anyways, I have a question. In all your articles you mention that the [[Scope]] variable is saved and does not change after creation. So, at what stage the creation of the function is performed. Code parsing?</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmitry A. Soshnikov</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/comment-page-1/#comment-2664</link>
		<dc:creator>Dmitry A. Soshnikov</dc:creator>
		<pubDate>Mon, 06 Sep 2010 17:09:32 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=118#comment-2664</guid>
		<description>@&lt;b&gt;gniavaj&lt;/b&gt;

&lt;blockquote&gt;we can say the function a create a “a closure”,but running the a function
dosen’t use the closure technology.&lt;/blockquote&gt;

You used a &lt;em&gt;practical&lt;/em&gt; definition of closures. However, as it was mentioned, &lt;em&gt;theoretically&lt;/em&gt; in ECMAScript &lt;em&gt;all functions are closures&lt;/em&gt;. Because for all functions at their &lt;em&gt;creation&lt;/em&gt; a parent scope chain is saved (this saved scope chain is stored in the [[Scope]] property of functions). I.e. a function possibly won&#039;t be even activated, but the [[Scope]] is already saved &lt;em&gt;at creation&lt;/em&gt;.

A normal global function if uses some global variable is also a closure (though, it&#039;s not so obvious until we won&#039;t have an ambiguous case):

[js]var foo = 10;

function bar() {
  console.log(foo);
}

bar(); // 10[/js]

Interesting? Not so (because it&#039;s just an access to the global variable). However, if we pass the same &quot;bar&quot; function as a &quot;funarg&quot;, we see the &lt;em&gt;closure nature&lt;/em&gt; (i.e. statically saved parent scope chain -- in which &quot;bar&quot; is created, but not when it&#039;s executed):

[js]var foo = 10;

function bar() {
  console.log(foo);
}

function bar(funArg) {
  // local &quot;foo&quot;
  var foo = 20;
  funArg(); // 10 (closured), but not 20
}

baz(bar); // 10[/js]

If a function doesn&#039;t use free variables (as in your example with &quot;a&quot; function), there may be a case with e.g. third-level inner function which &lt;em&gt;does use&lt;/em&gt; free variables.

[js](function foo() {

  // a variable of the
  // &quot;foo&quot; function context
  var x = 10;

  // function &quot;bar&quot; does not
  // use free variables. Should it
  // save parent (i.e. &quot;foo&quot;&#039;s) scope
  // chain and be a closure?
  
  (function bar() {
  
    // however, &quot;bar&quot; has another inner
    // function &quot;baz&quot; which uses &quot;x&quot; of &quot;foo&quot;

    (function baz() {
      console.log(x); // 10
    })();  

  })();
  
})();[/js]

And since a [[Scope]] is saved at creation, &quot;bar&quot; should save scope chain of &quot;foo&quot; anyway. Because when &quot;baz&quot; is created (at &quot;bar&quot; activation), it takes scope chain of the context in which it is created. And it&#039;s created in the &quot;bar&quot;. More exactly this case may be seen, if first to return &quot;bar&quot; to the outside, when context of &quot;foo&quot; will be already destroyed.

However, in real practice, if there is some &lt;em&gt;completely safe case&lt;/em&gt; (as again with your &quot;a&quot; function), I think implementations may do some optimization and do not save parent&#039;s scope chain and to used even stack but not &quot;heap&quot; (as I wrote in the article). But, it&#039;s already an implementation level and is not described in the ECMA-262. Also more complex parsing rules are needed.

&lt;blockquote&gt;dose this code use the closure technology?&lt;/blockquote&gt;

Possibly I used not so good example. However, yes, it&#039;s may be (even in practice) also a closure (in theory, repeat, it is a closure anyway). For example (abstractly, not regarding particular implementation in ECMAScript), &lt;em&gt;sort&lt;/em&gt; method may be deffered, i.e. activated after some time delay. Thus, if a sort-funarg uses some free variable from parent context, it should be a closure. Or, e.g. a sort-funarg simply may be passed as other function with also free variables:

[js]var someFreeVariable = true;

function sortFn(a, b) {
  if (someFreeVariable) {
    return a &lt; b ? 0 : 1;
  }
  return -1;
}

(function foo() {

  // another variable with
  // the same name
  var someFreeVariable = false;

  // however, sort uses &quot;sortFn&quot;
  // with &quot;someFreeVariable&quot; == true
  return [1, 2, 3].sort(sortFn);

})();[/js]

Dmitry.</description>
		<content:encoded><![CDATA[<p>@<b>gniavaj</b></p>
<blockquote><p>we can say the function a create a “a closure”,but running the a function<br />
dosen’t use the closure technology.</p></blockquote>
<p>You used a <em>practical</em> definition of closures. However, as it was mentioned, <em>theoretically</em> in ECMAScript <em>all functions are closures</em>. Because for all functions at their <em>creation</em> a parent scope chain is saved (this saved scope chain is stored in the [[Scope]] property of functions). I.e. a function possibly won&#8217;t be even activated, but the [[Scope]] is already saved <em>at creation</em>.</p>
<p>A normal global function if uses some global variable is also a closure (though, it&#8217;s not so obvious until we won&#8217;t have an ambiguous case):</p>
<pre class="brush: jscript; title: ;">var foo = 10;

function bar() {
  console.log(foo);
}

bar(); // 10</pre>
<p>Interesting? Not so (because it&#8217;s just an access to the global variable). However, if we pass the same &#8220;bar&#8221; function as a &#8220;funarg&#8221;, we see the <em>closure nature</em> (i.e. statically saved parent scope chain &#8212; in which &#8220;bar&#8221; is created, but not when it&#8217;s executed):</p>
<pre class="brush: jscript; title: ;">var foo = 10;

function bar() {
  console.log(foo);
}

function bar(funArg) {
  // local &quot;foo&quot;
  var foo = 20;
  funArg(); // 10 (closured), but not 20
}

baz(bar); // 10</pre>
<p>If a function doesn&#8217;t use free variables (as in your example with &#8220;a&#8221; function), there may be a case with e.g. third-level inner function which <em>does use</em> free variables.</p>
<pre class="brush: jscript; title: ;">(function foo() {

  // a variable of the
  // &quot;foo&quot; function context
  var x = 10;

  // function &quot;bar&quot; does not
  // use free variables. Should it
  // save parent (i.e. &quot;foo&quot;'s) scope
  // chain and be a closure?

  (function bar() {

    // however, &quot;bar&quot; has another inner
    // function &quot;baz&quot; which uses &quot;x&quot; of &quot;foo&quot;

    (function baz() {
      console.log(x); // 10
    })();  

  })();

})();</pre>
<p>And since a [[Scope]] is saved at creation, &#8220;bar&#8221; should save scope chain of &#8220;foo&#8221; anyway. Because when &#8220;baz&#8221; is created (at &#8220;bar&#8221; activation), it takes scope chain of the context in which it is created. And it&#8217;s created in the &#8220;bar&#8221;. More exactly this case may be seen, if first to return &#8220;bar&#8221; to the outside, when context of &#8220;foo&#8221; will be already destroyed.</p>
<p>However, in real practice, if there is some <em>completely safe case</em> (as again with your &#8220;a&#8221; function), I think implementations may do some optimization and do not save parent&#8217;s scope chain and to used even stack but not &#8220;heap&#8221; (as I wrote in the article). But, it&#8217;s already an implementation level and is not described in the ECMA-262. Also more complex parsing rules are needed.</p>
<blockquote><p>dose this code use the closure technology?</p></blockquote>
<p>Possibly I used not so good example. However, yes, it&#8217;s may be (even in practice) also a closure (in theory, repeat, it is a closure anyway). For example (abstractly, not regarding particular implementation in ECMAScript), <em>sort</em> method may be deffered, i.e. activated after some time delay. Thus, if a sort-funarg uses some free variable from parent context, it should be a closure. Or, e.g. a sort-funarg simply may be passed as other function with also free variables:</p>
<pre class="brush: jscript; title: ;">var someFreeVariable = true;

function sortFn(a, b) {
  if (someFreeVariable) {
    return a &lt; b ? 0 : 1;
  }
  return -1;
}

(function foo() {

  // another variable with
  // the same name
  var someFreeVariable = false;

  // however, sort uses &quot;sortFn&quot;
  // with &quot;someFreeVariable&quot; == true
  return [1, 2, 3].sort(sortFn);

})();</pre>
<p>Dmitry.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gniavaj</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/comment-page-1/#comment-2651</link>
		<dc:creator>gniavaj</dc:creator>
		<pubDate>Mon, 06 Sep 2010 06:53:23 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=118#comment-2651</guid>
		<description>thanks for your reply.
I still have a little confusion.
the closure we can think it is a structure.
it contains the function and the context&#039;s scope chain.
if a function like this 
[js]function a(){
    var b=&#039;&#039;;
    alert(b);
}[/js]
we can say the function a create a &quot;a closure&quot;,but running the a function
dosen&#039;t use the closure technology.

&lt;blockquote&gt;* exist even if context in which they were created is already finished (e.g. are the functional values returned from function);
 * reference in a code to free variables.&lt;/blockquote&gt;

if one code which meets one of the condition above,we can say the code use the closure technology.

but if i am correct,in your articel 

&lt;blockquote&gt;Practical usage of closures&lt;/blockquote&gt;

[js][1, 2, 3].sort(function (a, b) {
  ... // sort conditions
});[/js]

dose this code use the closure technology?</description>
		<content:encoded><![CDATA[<p>thanks for your reply.<br />
I still have a little confusion.<br />
the closure we can think it is a structure.<br />
it contains the function and the context&#8217;s scope chain.<br />
if a function like this </p>
<pre class="brush: jscript; title: ;">function a(){
    var b='';
    alert(b);
}</pre>
<p>we can say the function a create a &#8220;a closure&#8221;,but running the a function<br />
dosen&#8217;t use the closure technology.</p>
<blockquote><p>* exist even if context in which they were created is already finished (e.g. are the functional values returned from function);<br />
 * reference in a code to free variables.</p></blockquote>
<p>if one code which meets one of the condition above,we can say the code use the closure technology.</p>
<p>but if i am correct,in your articel </p>
<blockquote><p>Practical usage of closures</p></blockquote>
<pre class="brush: jscript; title: ;">[1, 2, 3].sort(function (a, b) {
  ... // sort conditions
});</pre>
<p>dose this code use the closure technology?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmitry A. Soshnikov</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/comment-page-1/#comment-2627</link>
		<dc:creator>Dmitry A. Soshnikov</dc:creator>
		<pubDate>Sun, 05 Sep 2010 19:00:50 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=118#comment-2627</guid>
		<description>@&lt;b&gt;gniavaj&lt;/b&gt;

&lt;blockquote&gt;“a closured lexical context” is the execution context?&lt;/blockquote&gt;

Nope, a &quot;closured context&quot; here means a term mostly from the general theory. Regarding ECMAScript it means a &lt;em&gt;&quot;parent variable object&quot;&lt;/em&gt; and, more closer, a complete &lt;em&gt;&quot;parent scope chain&quot;&lt;/em&gt;. I think I should correct terminology used in this article to better conform ES terminology.

&lt;blockquote&gt;if i am wrong,can you explain for me? thanks&lt;/blockquote&gt;

You are correct. However, it&#039;s not a whole context (scope chain, variable object, &lt;em&gt;this&lt;/em&gt; value) is saved, but only context&#039;s scope chain (as you know a &lt;em&gt;this value&lt;/em&gt; in contrast with other languages, e.g. Ruby or Python, isn&#039;t captured into the closure).

Dmitry.</description>
		<content:encoded><![CDATA[<p>@<b>gniavaj</b></p>
<blockquote><p>“a closured lexical context” is the execution context?</p></blockquote>
<p>Nope, a &#8220;closured context&#8221; here means a term mostly from the general theory. Regarding ECMAScript it means a <em>&#8220;parent variable object&#8221;</em> and, more closer, a complete <em>&#8220;parent scope chain&#8221;</em>. I think I should correct terminology used in this article to better conform ES terminology.</p>
<blockquote><p>if i am wrong,can you explain for me? thanks</p></blockquote>
<p>You are correct. However, it&#8217;s not a whole context (scope chain, variable object, <em>this</em> value) is saved, but only context&#8217;s scope chain (as you know a <em>this value</em> in contrast with other languages, e.g. Ruby or Python, isn&#8217;t captured into the closure).</p>
<p>Dmitry.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gniavaj</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/comment-page-1/#comment-2618</link>
		<dc:creator>gniavaj</dc:creator>
		<pubDate>Sun, 05 Sep 2010 15:33:41 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=118#comment-2618</guid>
		<description>Hi Dmitry.

&lt;blockquote&gt;Therefore, in this case as a rule, a closured lexical context is stored in the dynamic memory allocation (“heap”, so-called heap-based implementations), with using the garbage collector (GC) and references counting.&lt;/blockquote&gt;

&quot;a closured lexical context&quot; is the execution context?

[js](function(){
    var a=0;
    var b=1;
    var c=2;
    ....

    function test(){ alert(a) }
    return test;
})()[/js]

i use the program above to avoid the variables conflict.

when the anonymous function finish,the variable a still can be accessed.
this is because the global object refers the test function,
and the test function refers the anonymous function&#039;s execution context so the anonymous function&#039;s execution context wont&#039;t be GC.

if i am wrong,can you explain for me? thanks</description>
		<content:encoded><![CDATA[<p>Hi Dmitry.</p>
<blockquote><p>Therefore, in this case as a rule, a closured lexical context is stored in the dynamic memory allocation (“heap”, so-called heap-based implementations), with using the garbage collector (GC) and references counting.</p></blockquote>
<p>&#8220;a closured lexical context&#8221; is the execution context?</p>
<pre class="brush: jscript; title: ;">(function(){
    var a=0;
    var b=1;
    var c=2;
    ....

    function test(){ alert(a) }
    return test;
})()</pre>
<p>i use the program above to avoid the variables conflict.</p>
<p>when the anonymous function finish,the variable a still can be accessed.<br />
this is because the global object refers the test function,<br />
and the test function refers the anonymous function&#8217;s execution context so the anonymous function&#8217;s execution context wont&#8217;t be GC.</p>
<p>if i am wrong,can you explain for me? thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: denisdeng</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/comment-page-1/#comment-513</link>
		<dc:creator>denisdeng</dc:creator>
		<pubDate>Sun, 23 May 2010 01:43:30 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=118#comment-513</guid>
		<description>very good,thanks</description>
		<content:encoded><![CDATA[<p>very good,thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmitry A. Soshnikov</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/comment-page-1/#comment-511</link>
		<dc:creator>Dmitry A. Soshnikov</dc:creator>
		<pubDate>Sat, 22 May 2010 16:51:39 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=118#comment-511</guid>
		<description>@&lt;b&gt;denisdeng&lt;/b&gt;

&lt;blockquote&gt;&lt;blockquote&gt;for example, in Ruby programming language, as a closure a procedure object, a lambda-expression or a code block can be used&lt;/blockquote&gt;

I didn’t understand completely this sentence.Can you give me a detailed  explanation? thank you!&lt;/blockquote&gt;

There I just explain that in Ruby there are several ways to create a closure -- to show, that a &lt;em&gt;closure&lt;/em&gt; isn&#039;t required to be exactly a &lt;em&gt;function&lt;/em&gt;:

[ruby]a = 10

def foo(&amp;code_block)
  a = 20
  code_block.call # 10 (statically closured), but not 20
end

# lambda - is a closure
closure = lambda { print(a) }
foo(&amp;closure) # 10

# Proc.new - is also a closure
also_closure = Proc.new { print(a) }
foo(&amp;also_closure) # 10

# simple code block - {} or do/end
# is also a closure
foo { print(a) } # 10[/ruby]

Every of described approaches has its own features -- e.g. some created callable object checks quantity of passed parameters, some -- does not, or differently handles &lt;em&gt;return&lt;/em&gt; statement. Besides there are other ways to create a closure in Ruby, e.g. to use &lt;em&gt;method&lt;/em&gt; function applied for some method -- you can find details in documentation for Ruby.

But my the main idea was to show, that a &lt;em&gt;closure&lt;/em&gt; concept isn&#039;t requited to be related exactly with a &lt;em&gt;function&lt;/em&gt; concept. Although, in ECMAScript it is so.

Dmitry.</description>
		<content:encoded><![CDATA[<p>@<b>denisdeng</b></p>
<blockquote><blockquote>for example, in Ruby programming language, as a closure a procedure object, a lambda-expression or a code block can be used</p></blockquote>
<p>I didn’t understand completely this sentence.Can you give me a detailed  explanation? thank you!</p></blockquote>
<p>There I just explain that in Ruby there are several ways to create a closure &#8212; to show, that a <em>closure</em> isn&#8217;t required to be exactly a <em>function</em>:</p>
<pre class="brush: ruby; title: ;">a = 10

def foo(&amp;code_block)
  a = 20
  code_block.call # 10 (statically closured), but not 20
end

# lambda - is a closure
closure = lambda { print(a) }
foo(&amp;closure) # 10

# Proc.new - is also a closure
also_closure = Proc.new { print(a) }
foo(&amp;also_closure) # 10

# simple code block - {} or do/end
# is also a closure
foo { print(a) } # 10</pre>
<p>Every of described approaches has its own features &#8212; e.g. some created callable object checks quantity of passed parameters, some &#8212; does not, or differently handles <em>return</em> statement. Besides there are other ways to create a closure in Ruby, e.g. to use <em>method</em> function applied for some method &#8212; you can find details in documentation for Ruby.</p>
<p>But my the main idea was to show, that a <em>closure</em> concept isn&#8217;t requited to be related exactly with a <em>function</em> concept. Although, in ECMAScript it is so.</p>
<p>Dmitry.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: denisdeng</title>
		<link>http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/comment-page-1/#comment-509</link>
		<dc:creator>denisdeng</dc:creator>
		<pubDate>Sat, 22 May 2010 14:22:58 +0000</pubDate>
		<guid isPermaLink="false">http://dmitrysoshnikov.com/?p=118#comment-509</guid>
		<description>hi,Dmitry.
&lt;blockquote&gt;for example, in Ruby programming language, as a closure a procedure object, a lambda-expression or a code block can be used&lt;/blockquote&gt;

I didn’t understand completely this sentence.Can you give me a detailed  explanation? thank you!</description>
		<content:encoded><![CDATA[<p>hi,Dmitry.</p>
<blockquote><p>for example, in Ruby programming language, as a closure a procedure object, a lambda-expression or a code block can be used</p></blockquote>
<p>I didn’t understand completely this sentence.Can you give me a detailed  explanation? thank you!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

