
<?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: Javascript Gotchas listed to help avoid mistakes</title>
	<atom:link href="http://www.webdigi.co.uk/blog/2009/javascript-gotchas-listed-to-help-avoid-mistakes/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webdigi.co.uk/blog/2009/javascript-gotchas-listed-to-help-avoid-mistakes/</link>
	<description>Little words of wisdom</description>
	<lastBuildDate>Sat, 04 Sep 2010 23:02:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Does actionscript have javascript&#8217;s gotchas? &#124; Coppery Keen Claws</title>
		<link>http://www.webdigi.co.uk/blog/2009/javascript-gotchas-listed-to-help-avoid-mistakes/comment-page-1/#comment-20092</link>
		<dc:creator>Does actionscript have javascript&#8217;s gotchas? &#124; Coppery Keen Claws</dc:creator>
		<pubDate>Fri, 03 Sep 2010 02:48:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.webdigi.co.uk/blog/?p=361#comment-20092</guid>
		<description>[...] usual advice to use static typing. I was curious if some of javascript&#8217;s peculiarities (like these and these) that trip up beginners come up in actionscript, [...]</description>
		<content:encoded><![CDATA[<p>[...] usual advice to use static typing. I was curious if some of javascript&#8217;s peculiarities (like these and these) that trip up beginners come up in actionscript, [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon Proctor</title>
		<link>http://www.webdigi.co.uk/blog/2009/javascript-gotchas-listed-to-help-avoid-mistakes/comment-page-1/#comment-2954</link>
		<dc:creator>Simon Proctor</dc:creator>
		<pubDate>Tue, 26 May 2009 14:00:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.webdigi.co.uk/blog/?p=361#comment-2954</guid>
		<description>One gotcha that&#039;s more of a DOM issue than a Javascript one is to do with DOM0 and form.submit().

Basically any inputs you put into a form will be available in that form using the form.X where X is the inputs name. So if you have a submit button and you call it submit (the name not the type) this will override form.submit making the function unavailable to call.</description>
		<content:encoded><![CDATA[<p>One gotcha that&#8217;s more of a DOM issue than a Javascript one is to do with DOM0 and form.submit().</p>
<p>Basically any inputs you put into a form will be available in that form using the form.X where X is the inputs name. So if you have a submit button and you call it submit (the name not the type) this will override form.submit making the function unavailable to call.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bryan Migliorisi</title>
		<link>http://www.webdigi.co.uk/blog/2009/javascript-gotchas-listed-to-help-avoid-mistakes/comment-page-1/#comment-2876</link>
		<dc:creator>Bryan Migliorisi</dc:creator>
		<pubDate>Mon, 25 May 2009 03:33:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webdigi.co.uk/blog/?p=361#comment-2876</guid>
		<description>For #6, you can make JavaScript replace all by using the following syntax:

&quot;I dont need spaces&quot;.replace(&quot; &quot;,&quot;_&quot;, &quot;ig&quot;);  

The 3rd paramter is the Regular expression modifier that tells it how to do the replacement.

i = case Insensitive
g = global (aka all matches)</description>
		<content:encoded><![CDATA[<p>For #6, you can make JavaScript replace all by using the following syntax:</p>
<p>&#8220;I dont need spaces&#8221;.replace(&#8221; &#8220;,&#8221;_&#8221;, &#8220;ig&#8221;);  </p>
<p>The 3rd paramter is the Regular expression modifier that tells it how to do the replacement.</p>
<p>i = case Insensitive<br />
g = global (aka all matches)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://www.webdigi.co.uk/blog/2009/javascript-gotchas-listed-to-help-avoid-mistakes/comment-page-1/#comment-2841</link>
		<dc:creator>John</dc:creator>
		<pubDate>Sun, 24 May 2009 10:24:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.webdigi.co.uk/blog/?p=361#comment-2841</guid>
		<description>As mentioned, the floating point stuff is not JavaScript specific. Here are two of the biggest fails I routinely see in other people&#039;s code:

1. Not declaring local variables. When JavaScript encounters an undeclared variable, it will go searching up the scope chain.

function myFunction (x) {
    for (i = 0 ; i &lt; x ; i++) {
      foo () ;
    }
}

for (i = 0 ; i &lt; 10 ; i++) {
    myFunction (i) ;
}

In the above code, the &quot;i&quot; in myFunction() will refer to the global &quot;i&quot; producing some very odd results.

2. Using &quot;for .. in&quot; on arrays. &quot;for .. in&quot; is for iterating over OBJECT properties, not array values. In JavaScript, arrays are objects and &quot;length&quot; is a property of an array:

var a = [&quot;hello&quot;, &quot;world&quot;] ;
for (var i in a) {
    alert (i + &#039;: &#039; + a[i]) ;
}

The above code will result in three alerts, not the two that most people expect.</description>
		<content:encoded><![CDATA[<p>As mentioned, the floating point stuff is not JavaScript specific. Here are two of the biggest fails I routinely see in other people&#8217;s code:</p>
<p>1. Not declaring local variables. When JavaScript encounters an undeclared variable, it will go searching up the scope chain.</p>
<p>function myFunction (x) {<br />
    for (i = 0 ; i &lt; x ; i++) {<br />
      foo () ;<br />
    }<br />
}</p>
<p>for (i = 0 ; i &lt; 10 ; i++) {<br />
    myFunction (i) ;<br />
}</p>
<p>In the above code, the &#8220;i&#8221; in myFunction() will refer to the global &#8220;i&#8221; producing some very odd results.</p>
<p>2. Using &#8220;for .. in&#8221; on arrays. &#8220;for .. in&#8221; is for iterating over OBJECT properties, not array values. In JavaScript, arrays are objects and &#8220;length&#8221; is a property of an array:</p>
<p>var a = ["hello", "world"] ;<br />
for (var i in a) {<br />
    alert (i + &#8216;: &#8216; + a[i]) ;<br />
}</p>
<p>The above code will result in three alerts, not the two that most people expect.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://www.webdigi.co.uk/blog/2009/javascript-gotchas-listed-to-help-avoid-mistakes/comment-page-1/#comment-2838</link>
		<dc:creator>James</dc:creator>
		<pubDate>Sun, 24 May 2009 08:36:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.webdigi.co.uk/blog/?p=361#comment-2838</guid>
		<description>Alex, as established, JavaScript will insert semi-colons at the end of &quot;statements&quot; if they&#039;re not there already. In your example, arg1, arg2 and arg3 are not statements - they&#039;re &quot;expressions&quot;; JavaScript knows this and so doesn&#039;t bother inserting any semi colons. Although you are right, #3 states that semi-colons are inserted at line-feeds; which is obviously incorrect.</description>
		<content:encoded><![CDATA[<p>Alex, as established, JavaScript will insert semi-colons at the end of &#8220;statements&#8221; if they&#8217;re not there already. In your example, arg1, arg2 and arg3 are not statements &#8211; they&#8217;re &#8220;expressions&#8221;; JavaScript knows this and so doesn&#8217;t bother inserting any semi colons. Although you are right, #3 states that semi-colons are inserted at line-feeds; which is obviously incorrect.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pinderkent: Programming languages should not try to guess the programmer&apos;s intentions.</title>
		<link>http://www.webdigi.co.uk/blog/2009/javascript-gotchas-listed-to-help-avoid-mistakes/comment-page-1/#comment-2823</link>
		<dc:creator>Pinderkent: Programming languages should not try to guess the programmer&apos;s intentions.</dc:creator>
		<pubDate>Sun, 24 May 2009 01:03:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.webdigi.co.uk/blog/?p=361#comment-2823</guid>
		<description>[...] is another language that employs weak, dynamic typing. I recently saw another article that gives some good examples (under the &quot;2. Plus operator overloading&quot; section) of how this [...]</description>
		<content:encoded><![CDATA[<p>[...] is another language that employs weak, dynamic typing. I recently saw another article that gives some good examples (under the &#8220;2. Plus operator overloading&#8221; section) of how this [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: links for 2009-05-21 &#171; My Weblog</title>
		<link>http://www.webdigi.co.uk/blog/2009/javascript-gotchas-listed-to-help-avoid-mistakes/comment-page-1/#comment-2732</link>
		<dc:creator>links for 2009-05-21 &#171; My Weblog</dc:creator>
		<pubDate>Fri, 22 May 2009 04:39:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.webdigi.co.uk/blog/?p=361#comment-2732</guid>
		<description>[...] » Javascript Gotchas listed to help avoid mistakes &#8211; PHP, Web and IT stuff (tags: javascript) [...]</description>
		<content:encoded><![CDATA[<p>[...] » Javascript Gotchas listed to help avoid mistakes &#8211; PHP, Web and IT stuff (tags: javascript) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Memet</title>
		<link>http://www.webdigi.co.uk/blog/2009/javascript-gotchas-listed-to-help-avoid-mistakes/comment-page-1/#comment-2725</link>
		<dc:creator>Memet</dc:creator>
		<pubDate>Fri, 22 May 2009 01:24:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.webdigi.co.uk/blog/?p=361#comment-2725</guid>
		<description>@John Dehope: &quot;They expect a + b to be the same as b + a. Really it should be shouldn’t it?&quot;

 It&#039;s not even programming languages, it&#039;s the IEEE standard. There&#039;s a very basic reason for it when you think about it for 10 seconds.</description>
		<content:encoded><![CDATA[<p>@John Dehope: &#8220;They expect a + b to be the same as b + a. Really it should be shouldn’t it?&#8221;</p>
<p> It&#8217;s not even programming languages, it&#8217;s the IEEE standard. There&#8217;s a very basic reason for it when you think about it for 10 seconds.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dblackshell</title>
		<link>http://www.webdigi.co.uk/blog/2009/javascript-gotchas-listed-to-help-avoid-mistakes/comment-page-1/#comment-2724</link>
		<dc:creator>dblackshell</dc:creator>
		<pubDate>Fri, 22 May 2009 00:52:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.webdigi.co.uk/blog/?p=361#comment-2724</guid>
		<description>#6 well yes, because Javascript uses regular expressions for replacing...</description>
		<content:encoded><![CDATA[<p>#6 well yes, because Javascript uses regular expressions for replacing&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.webdigi.co.uk/blog/2009/javascript-gotchas-listed-to-help-avoid-mistakes/comment-page-1/#comment-2714</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Thu, 21 May 2009 20:23:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.webdigi.co.uk/blog/?p=361#comment-2714</guid>
		<description>#3 is not strictly correct.  If it were then doing this would be a problem:
myFunc(arg1,
   arg2,
   arg3);

JavaScript automatically inserts semi-colons after _some_ statements, the return statement being the classic example.  And this is why it is good practice in JavaScript to put the opening brace on the same line as the preceding statement.
e.g. This returns a object as expected:
return  {
  key: value
}
This returns undefined because a semi-colon is inserted after the return statement:
return
{ key : value }</description>
		<content:encoded><![CDATA[<p>#3 is not strictly correct.  If it were then doing this would be a problem:<br />
myFunc(arg1,<br />
   arg2,<br />
   arg3);</p>
<p>JavaScript automatically inserts semi-colons after _some_ statements, the return statement being the classic example.  And this is why it is good practice in JavaScript to put the opening brace on the same line as the preceding statement.<br />
e.g. This returns a object as expected:<br />
return  {<br />
  key: value<br />
}<br />
This returns undefined because a semi-colon is inserted after the return statement:<br />
return<br />
{ key : value }</p>
]]></content:encoded>
	</item>
</channel>
</rss>
