<?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: 3 ways of creating functions in javascript</title>
	<atom:link href="http://www.javascriptkata.com/2007/03/26/3-ways-of-creating-functions-in-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com/2007/03/26/3-ways-of-creating-functions-in-javascript/</link>
	<description>Advanced katas for javascripters</description>
	<lastBuildDate>Fri, 30 Jul 2010 18:10:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: The power of closures in javascript &#124; Javascript Kata</title>
		<link>http://www.javascriptkata.com/2007/03/26/3-ways-of-creating-functions-in-javascript/comment-page-1/#comment-781</link>
		<dc:creator>The power of closures in javascript &#124; Javascript Kata</dc:creator>
		<pubDate>Tue, 22 Sep 2009 03:42:59 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=16#comment-781</guid>
		<description>[...] Attention! Before reading this article, you should take a look at 3 ways of creating functions in javascript. [...]</description>
		<content:encoded><![CDATA[<p>[...] Attention! Before reading this article, you should take a look at 3 ways of creating functions in javascript. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://www.javascriptkata.com/2007/03/26/3-ways-of-creating-functions-in-javascript/comment-page-1/#comment-43</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Wed, 18 Apr 2007 17:12:58 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=16#comment-43</guid>
		<description>Yeah, I like that approach -- by making the form a property of the image, I get a the processing of the id value over and done with before I actually generate the function code.  That approach would also streamline a few other things going on in my app.</description>
		<content:encoded><![CDATA[<p>Yeah, I like that approach &#8212; by making the form a property of the image, I get a the processing of the id value over and done with before I actually generate the function code.  That approach would also streamline a few other things going on in my app.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BK</title>
		<link>http://www.javascriptkata.com/2007/03/26/3-ways-of-creating-functions-in-javascript/comment-page-1/#comment-42</link>
		<dc:creator>BK</dc:creator>
		<pubDate>Mon, 16 Apr 2007 17:22:12 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=16#comment-42</guid>
		<description>Since you get the information as XML, you necessarily have to parse it using JS. What you then could do is something like:

var myImg=document.createElement(&quot;img&quot;)
img.src=&quot;mistigri.jpg&quot;
img.refForm=document.forms[0]
img.save=function(){this.refForm.save.click()}
img.onclick=this.save;</description>
		<content:encoded><![CDATA[<p>Since you get the information as XML, you necessarily have to parse it using JS. What you then could do is something like:</p>
<p>var myImg=document.createElement(&#8220;img&#8221;)<br />
img.src=&#8221;mistigri.jpg&#8221;<br />
img.refForm=document.forms[0]<br />
img.save=function(){this.refForm.save.click()}<br />
img.onclick=this.save;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://www.javascriptkata.com/2007/03/26/3-ways-of-creating-functions-in-javascript/comment-page-1/#comment-41</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Mon, 16 Apr 2007 15:32:07 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=16#comment-41</guid>
		<description>Hmmm, I&#039;m trying to figure out if they will help.  I have heard that they use up a lot of memory, and it seems like I would need to keep a lot of objects hanging around if I want to use this multiple times.

The basic problem I have is that I&#039;ve got a page with what Access calls &quot;continuous forms&quot; (I call it a paper towel page, because there are a series of identical forms, each one displaying one record).

I&#039;ve switched to using AJAX, so I need to rewrite the page and insert a node with a new form each time a record is added.  There is a blank form at the end for this purpose.  When its save button is clicked, the div node holding the form is cloned, and I copy the element contents to the new elements (interesting that a &quot;deep&quot; clone doesn&#039;t do this ...).

Then I rename and re-id each element.  The problem comes in assigning the event handlers, since I&#039;ve got to do it in a

  frm[&quot;saveButton&quot;].onclick = ...

fashion, and the code has to use the id field value that came back with the XML response (note that the response is true XML, not HTML, which would make life easier since the script could be written with the ids embedded at the server end).

For actual form elements, that works fine, because the id value is stored in a hidden field in the form, so I can use a function() { ... } approach, since the code is plain text (using frm.id.value to get the id).  But, for example, an image clicked to collapse or expand the div can&#039;t reference the form easily; I&#039;d have to go backing up the node tree and coming down into the form.

So, I use:


  imgShowHide.onclick = new Function(&quot;toggleDisplay(&quot; + intId + &quot;);&quot;);


Once the string is generated, that id value stays with the function and is protected from any changes.</description>
		<content:encoded><![CDATA[<p>Hmmm, I&#8217;m trying to figure out if they will help.  I have heard that they use up a lot of memory, and it seems like I would need to keep a lot of objects hanging around if I want to use this multiple times.</p>
<p>The basic problem I have is that I&#8217;ve got a page with what Access calls &#8220;continuous forms&#8221; (I call it a paper towel page, because there are a series of identical forms, each one displaying one record).</p>
<p>I&#8217;ve switched to using AJAX, so I need to rewrite the page and insert a node with a new form each time a record is added.  There is a blank form at the end for this purpose.  When its save button is clicked, the div node holding the form is cloned, and I copy the element contents to the new elements (interesting that a &#8220;deep&#8221; clone doesn&#8217;t do this &#8230;).</p>
<p>Then I rename and re-id each element.  The problem comes in assigning the event handlers, since I&#8217;ve got to do it in a</p>
<p>  frm["saveButton"].onclick = &#8230;</p>
<p>fashion, and the code has to use the id field value that came back with the XML response (note that the response is true XML, not HTML, which would make life easier since the script could be written with the ids embedded at the server end).</p>
<p>For actual form elements, that works fine, because the id value is stored in a hidden field in the form, so I can use a function() { &#8230; } approach, since the code is plain text (using frm.id.value to get the id).  But, for example, an image clicked to collapse or expand the div can&#8217;t reference the form easily; I&#8217;d have to go backing up the node tree and coming down into the form.</p>
<p>So, I use:</p>
<p>  imgShowHide.onclick = new Function(&#8220;toggleDisplay(&#8221; + intId + &#8220;);&#8221;);</p>
<p>Once the string is generated, that id value stays with the function and is protected from any changes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2007/03/26/3-ways-of-creating-functions-in-javascript/comment-page-1/#comment-40</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Mon, 16 Apr 2007 13:39:06 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=16#comment-40</guid>
		<description>Steve, I suggest that you take a look at &lt;a href=&quot;http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/&quot; rel=&quot;nofollow&quot;&gt;closures&lt;/a&gt;. It could solve your two problems.

&lt;pre&gt;
var i = 10;
fct = function() {
  alert(i);
}

fct();
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Steve, I suggest that you take a look at <a href="http://www.javascriptkata.com/2007/04/10/the-power-of-closures-in-javascript/" rel="nofollow">closures</a>. It could solve your two problems.</p>
<pre>
var i = 10;
fct = function() {
  alert(i);
}

fct();
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://www.javascriptkata.com/2007/03/26/3-ways-of-creating-functions-in-javascript/comment-page-1/#comment-39</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Mon, 16 Apr 2007 03:32:35 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=16#comment-39</guid>
		<description>In your Function example, you concatenate the value of the variable i into the string.  And the value it had at that time remains permanently.  I haven&#039;t found any other good way to write that sort of thing other than as a new Function.

In your function example, the code is static -- there is no code generated which could differ depending on the situation.</description>
		<content:encoded><![CDATA[<p>In your Function example, you concatenate the value of the variable i into the string.  And the value it had at that time remains permanently.  I haven&#8217;t found any other good way to write that sort of thing other than as a new Function.</p>
<p>In your function example, the code is static &#8212; there is no code generated which could differ depending on the situation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2007/03/26/3-ways-of-creating-functions-in-javascript/comment-page-1/#comment-38</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Wed, 28 Mar 2007 04:53:35 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=16#comment-38</guid>
		<description>Frank, my mistake.

var alertNumber(â€alert(â€™Number is â€ + i + â€œ);â€);

was changed to

var  alertNumber = new Function(&quot;alert(&#039;Number is &quot; + i + &quot;&#039;);&quot;);</description>
		<content:encoded><![CDATA[<p>Frank, my mistake.</p>
<p>var alertNumber(â€alert(â€™Number is â€ + i + â€œ);â€);</p>
<p>was changed to</p>
<p>var  alertNumber = new Function(&#8220;alert(&#8216;Number is &#8221; + i + &#8220;&#8216;);&#8221;);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.javascriptkata.com/2007/03/26/3-ways-of-creating-functions-in-javascript/comment-page-1/#comment-37</link>
		<dc:creator>Frank</dc:creator>
		<pubDate>Tue, 27 Mar 2007 23:34:20 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=16#comment-37</guid>
		<description>Dan, there is one bit of code I don&#039;t understand.

   1. var i = 100;
   2. // &quot;dynamically&quot; alert the i variable
   3. var alertNumber(&quot;alert(&#039;Number is &quot; + i + &quot;);&quot;);

What is &quot;alertNumber&quot; exactly? I&#039;m confused as it&#039;s kind of came out from nowhere.
Is it an instance of the &quot;Function&quot; class?</description>
		<content:encoded><![CDATA[<p>Dan, there is one bit of code I don&#8217;t understand.</p>
<p>   1. var i = 100;<br />
   2. // &#8220;dynamically&#8221; alert the i variable<br />
   3. var alertNumber(&#8220;alert(&#8216;Number is &#8221; + i + &#8220;);&#8221;);</p>
<p>What is &#8220;alertNumber&#8221; exactly? I&#8217;m confused as it&#8217;s kind of came out from nowhere.<br />
Is it an instance of the &#8220;Function&#8221; class?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2007/03/26/3-ways-of-creating-functions-in-javascript/comment-page-1/#comment-36</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Tue, 27 Mar 2007 16:30:16 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=16#comment-36</guid>
		<description>Thanks Chris. I took a look at your site. Happy to see that I&#039;m not the only one working to make javascript a better place.

I used simple terms to make the distinction because not everyone is familiar with the more technical terms. But I have to admit that I should have referenced them... I will as soon as possible.</description>
		<content:encoded><![CDATA[<p>Thanks Chris. I took a look at your site. Happy to see that I&#8217;m not the only one working to make javascript a better place.</p>
<p>I used simple terms to make the distinction because not everyone is familiar with the more technical terms. But I have to admit that I should have referenced them&#8230; I will as soon as possible.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Lamothe</title>
		<link>http://www.javascriptkata.com/2007/03/26/3-ways-of-creating-functions-in-javascript/comment-page-1/#comment-35</link>
		<dc:creator>Chris Lamothe</dc:creator>
		<pubDate>Tue, 27 Mar 2007 15:53:23 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=16#comment-35</guid>
		<description>Your so called &quot;lowercase f way&quot; is properly refered to as an anonymous function, while your &quot;uppercase F way&quot; is the creation of a function through a constructor.  You&#039;re &quot;old way is probably best refered to ask a traditional function.  Fancy that.</description>
		<content:encoded><![CDATA[<p>Your so called &#8220;lowercase f way&#8221; is properly refered to as an anonymous function, while your &#8220;uppercase F way&#8221; is the creation of a function through a constructor.  You&#8217;re &#8220;old way is probably best refered to ask a traditional function.  Fancy that.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
