<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mis Movidas &#187; Uncategorized</title>
	<atom:link href="http://blog.mariorocafull.es/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mariorocafull.es</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 12 Jan 2012 08:56:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Functions that return functions.</title>
		<link>http://blog.mariorocafull.es/functions-that-return-functions/</link>
		<comments>http://blog.mariorocafull.es/functions-that-return-functions/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 09:52:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.mariorocafull.es/?p=366</guid>
		<description><![CDATA[If you don´t know anything about the scope of variables, stop reading. Why are they necessary? A tipical case is when a function expects a callback function as a parameter, for sample to notify an event, but you want to execute a method of an object (of a specific instance). That is what you cannot [...]]]></description>
			<content:encoded><![CDATA[<p>If you don´t know anything about the scope of variables, stop reading.</p>
<p><strong>Why are they necessary?</strong>
<p>A tipical case is when a function expects a callback function as a parameter, for sample to notify an event, but you want to execute a method of an object (of a specific instance).</p>
<p>That is what you <strong>cannot</strong> do:</p>
<p><code><br />
var oFool = {}<br />
oFool.value = 5;<br />
oFool.show = function () { alert(this.value) };<br />
setTimeout(oFool.show, 100);<br />
</code></p>
<p>In this case the function <em>setTimeout</em> is getting as a callback function the code of the function that acts as a method for the object, but out of the scope of that object, that is why the alert will show <em>undefined</em>.</p>
<p>In this case, the solution can be an anonimous:</p>
<p><code><br />
var oFool = {};<br />
oFool.value = 5;<br />
oFool.show = function () { alert(this.value) };<br />
setTimeout(function () {<br />
  oFool.show();<br />
}, 100);<br />
</code></p>
<p>This anonimous function is binded to the same scope that the code in which the function <em>setTimeout</em> is running (that no necessarly is the global scope) and thus it can access to the instance of <em>oFool</em>.</p>
<p><strong>¿When do you need a function that returns a function</strong> or <strong>when an anonimous function is not the proper solution?</strong></p>
<p>The anonimous function is not the proper solution when the binded scope is changing, for sample inside a loop, because in this case the function will remain binded with the last value of the iterator.</p>
<p><code><br />
var aLayers = document.getElementsByName("div");<br />
for(k in aLayers) {<br />
  var oLayer = aLayers[k];<br />
  oLayer.onclick = function () {<br />
    alert("div #"+k);<br />
  };<br />
}<br />
</code></p>
<p>In this case, all the functions binded to each layer will remain binded to the same scope, therefore the variable <em>k</em> will have the same value for all of them.</p>
<p>A proper solution, useful for more complex cases is:</p>
<p><code><br />
var aLayers = document.getElementsByName("div");<br />
for(k in aLayers) {<br />
  var oLayer = aLayers[k];<br />
  oLayer.num = k;<br />
  oLayer.onclick = function () {<br />
    alert("div #"+this.num);<br />
  };<br />
}<br />
</code></p>
<p>Because in this case, the value of the variable <em>k</em> is stored in the scope of each layer and the binded function makes use of this scope (by <em>this</em>).</p>
<p>But we have come here to show the use of a function that returns a function, so let us see a solution that makes use of this technic:</p>
<p><code><br />
var aLayers = document.getElementsByName("div");<br />
for(k in aLayers) {<br />
  var oLayer = aLayers[k];<br />
  var fClick = function (p) {<br />
    return function () {<br />
      alert(p);<br />
    };<br />
  };<br />
  oLayer.onclick = fClick(k);<br />
}<br />
</code></p>
<p>What is going on here is that the function <em>fClick</em> get a parameter <em>p</em> which becomes part of its scope and returns an anonimous function  that makes use of this parameter. When <em>fClick(k)</em> is binded to the <em>onclick</em> event, the value of <em>k</em> is binded to the scope of anonimous function that is created at that moment and doesn´t change in the next iteration, in which a new anonimous function will be created with a new scope that will have a different value for <em>k</em> in its variable <em>p</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mariorocafull.es/functions-that-return-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Saludos / Greetings / Grüße</title>
		<link>http://blog.mariorocafull.es/saludos-greetings-gruse/</link>
		<comments>http://blog.mariorocafull.es/saludos-greetings-gruse/#comments</comments>
		<pubDate>Wed, 18 Oct 2006 20:48:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://127.0.0.1/wp/?p=3</guid>
		<description><![CDATA[Bueno, parece que después de un tiempo de inactividad, me apetece volver a postear, lo cierto es que siempre hay cosas que decir, y lo más extraño, siempre hay alguien que acaba leyendolas. A todos ellos/as, gracias.]]></description>
			<content:encoded><![CDATA[<p>Bueno, parece que después de un tiempo de inactividad, me apetece volver a postear, lo cierto es que siempre hay cosas que decir, y lo más extraño, siempre hay alguien que acaba leyendolas. A todos ellos/as, gracias.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mariorocafull.es/saludos-greetings-gruse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

