<?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>James Wiseman &#187; Standards and Best Practice</title>
	<atom:link href="http://www.jameswiseman.com/blog/tag/standards-and-best-practice/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jameswiseman.com/blog</link>
	<description>My Programming Notebook and Other Things</description>
	<lastBuildDate>Mon, 06 Feb 2012 19:41:44 +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>JSLint Messages &#8211; Coding Convention and Style Guide</title>
		<link>http://www.jameswiseman.com/blog/2011/03/26/coding-convention-an-style-guide/</link>
		<comments>http://www.jameswiseman.com/blog/2011/03/26/coding-convention-an-style-guide/#comments</comments>
		<pubDate>Sat, 26 Mar 2011 11:30:52 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2570</guid>
		<description><![CDATA[Why Bother? JSLint contains a number of messages pertaining to the style, appearance and formatting of JavaScript code. This includes stipulations about bracing positions, indentation and spacing. As a demonstration of this paste the following into http://jslint.com/ var x = 0; switch(x){ case 1: x++; break; case 2: x--; break; default: break; } Zero errors, [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><h3>Why Bother?</h3>
<p>JSLint contains a number of messages pertaining to the style, appearance and formatting of JavaScript code.  This includes stipulations about bracing positions, indentation and spacing.</p>
<p>As a demonstration of this paste the following into <a href="http://jslint.com/">http://jslint.com/</a></p>
<pre name="code" class="javascript">var x = 0;
switch(x){
    case 1:
        x++;
        break;
    case 2:
        x--;
        break;
    default:
        break;
}</pre>
<p>Zero errors, right? Now click the flag &#8216;Strict white space&#8217; (&#8216;<strong>white&#8217;</strong>) and recheck&#8230;</p>
<p>Yikes! 10 errors (at the time of writing)!</p>
<p>So why should JSLint possibly care what your code looks like? After all, it&#8217;s all about bugs, isn&#8217;t it?</p>
<p>Well, on the surface, legible code equates to more maintainable code, which itself equates less ambiguity and fewer bugs. But you may have your own internal guidelines that dictate how the JavaScript looks, i.e. how you indent, where you put your braces, etc. So you will have probably flipped the &#8216;white&#8217; flag to &#8216;off&#8217;. After all, when given the above code snippet, JSLint returns a substantial amount of what you consider to be &#8216;noise&#8217;, which if no use to you. Right?</p>
<p>However in turning this option off, you are missing the opportunity to have a uniform and consistent convention across your code base, and a tool that can tell you where you are deviating from it.</p>
<p>But sometimes, formatting and positioning does have functional implication as well, and can lead itself to bugs. Consider the following code:</p>
<pre name="code" class="javascript">function GetObjectLiteral()
{
    return
   {
       x:0,
       y:1
   };
}
alert(GetObjectLiteral().x); //what do you expect?</pre>
<p>Regardless of what you expect the above to return, what you will <strong>not </strong>get is an alert box with the text &#8217;0&#8242;. Different browsers may behave differently. IE8 complains about a missing semi-colon, whilst others will simply execute the &#8216;return&#8217;, passing control back to the calling function, and then fail to find the member &#8216;a&#8217;</p>
<p>Changing the bracing position fixes the code:</p>
<pre name="code" class="javascript">function GetObjectLiteral(){
    return{
       x:0,
       y:1
   };
}
alert(GetObjectLiteral().x); //alerts '0'</pre>
<h3>The Messages</h3>
<p>So, let&#8217;s have a look at some of these messages&#8230;</p>
<p><strong>Expected ‘{a}’ at column {b}, not column {c}.</strong></p>
<p>This is a simple case of incorrect indentation. The most basic demonstration of this can be seen in following code snippet:</p>
<pre name="code" class="javascript">var a = 0;
  var b = 0; //Problem at line 2 character 3: Expected 'var' at column 1, not column 3</pre>
<p><strong>Expected ‘{a}’ to have an indentation of {b} instead of {c}.</strong></p>
<p><strong> </strong>This is another indentation catch. The default indentation step is 4, meaning that indentation columns should start at positions 1, 5, 9, 13, 17, etc. The sample below uses an indent of 5 spaces, placing the start of the new column at character 6 (as the message denotes).</p>
<pre name="code" class="javascript">function MyFunc() {
     alert("hello"); //Problem at line 2 character 6: Expected 'alert' at column 5, not column 6.
//3456789
}</pre>
<p><strong>Expected exactly one space between ‘{a}’ and ‘{b}’.</strong></p>
<p>This message goes some way to supporting JSLint&#8217;s assertion of correct bracing positions. You will see this message generated on the following code snippet.</p>
<pre name="code" class="javascript">if (x === 0)
{  //brace on next line
    alert("hello");
}</pre>
<p>But even when you do place the brace on the correct line, JSLint requires you to apply the correct spacing. So, the following code snippets will also yield this message:</p>
<pre name="code" class="javascript">if (x === 0){ //no spaces
    alert("hello");
}

if (x === 0)  {//two spaces
    alert("hello");
}</pre>
<p><strong>Missing space between &#8216;{a}&#8217; and &#8216;{b}&#8217;.</strong></p>
<p>Haveyoutriedreadingsentencesthathavetheirspacesomitted? Generally, it&#8217;s not that easy, is it?</p>
<pre name="code" class="javascript">if (x === 0){    //Missing space between ')' and '{'
    alert("hello");
}</pre>
<p><strong>Mixed spaces and tabs.</strong></p>
<p>This message is displayed when a line is indented with a mixture of spaces and tabs. Most IDEs will have an option to convert tabs into spaces automatically. I suggest you turn this on.</p>
<p><strong>Unexpected space between &#8216;{a}&#8217; and &#8216;{b}&#8217;</strong></p>
<p>Here, JSLint is reporting that you have included a space where it did not expect one. The following code snippet will yield this message:</p>
<pre name="code" class="javascript">if ( x === 0){  //Unexpected space between '(' and 'x'
    alert("hello");
}</pre>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/"> error and warning messages produced by JSLint</a>.</p>
<div class="shr-publisher-2570"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F03%2F26%2Fcoding-convention-an-style-guide%2F' data-shr_title='JSLint+Messages+-+Coding+Convention+and+Style+Guide'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F03%2F26%2Fcoding-convention-an-style-guide%2F' data-shr_title='JSLint+Messages+-+Coding+Convention+and+Style+Guide'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/03/26/coding-convention-an-style-guide/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>JSLint Messages &#8211; Do not wrap function literals in parens unless they are to be immediately invoked.</title>
		<link>http://www.jameswiseman.com/blog/2011/02/17/jslint-messages-do-not-wrap-function-literals-in-parens-unless-they-are-to-be-immediately-invoked/</link>
		<comments>http://www.jameswiseman.com/blog/2011/02/17/jslint-messages-do-not-wrap-function-literals-in-parens-unless-they-are-to-be-immediately-invoked/#comments</comments>
		<pubDate>Thu, 17 Feb 2011 17:40:21 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JavaScript-functions]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2210</guid>
		<description><![CDATA[In JavaScript we can assign a function expression to a variable, like so: var foo = function() { return 1; }; //assign function expression to 'foo' This is perfectly valid, and often useful. It is also possible to assign a variable the return value of the function expression by invoking the function, like so: var [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>In JavaScript we can assign a function expression to a variable, like so:</p>
<pre name="code" class="JavaScript">var foo = function() {
    return 1;
}; //assign function expression to 'foo'</pre>
<p>This is perfectly valid, and often useful.</p>
<p>It is also possible to assign a variable the return value of the function expression by invoking the function, like so:</p>
<pre name="code" class="JavaScript">var foo = (function() {
    return 1;
}()); //assign '1' to 'foo'</pre>
<p>This is also valid, although less commonly useful.</p>
<p>These are two distinct techniques with distinct conventions, so, the following may well give rise to confusion:</p>
<pre name="code" class="JavaScript">var foo = (function() {
    return 1;
});//assign function expression to 'foo'</pre>
<p>The above is equivalent to the first sample of code, and will execute in exactly the same way. JSLint <a title="Function statements are not invocable. Wrap the whole function invocation in parens." href="http://www.jameswiseman.com/blog/2011/02/15/jslint-messages-function-statements-are-not-invocable-wrap-the-entire-immediate-function-invocation-in-parens/" target="_blank">encourages you to wrap invoked function expressions with parentheses</a>. Wrapping non-invoked function expressions in the same way may be indicative of something non-intentional, or at very least be misinterpreted by a future developer.</p>
<p>In other words, stick to the first convention for the assignment of function expressions.</p>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/" target="_blank"> error and warning messages produced by JSLint</a>.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><img class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/pixy.gif?x-id=c2fc4320-3970-4e81-b251-8e4787a8cf17" alt="" /><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<div class="shr-publisher-2210"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F02%2F17%2Fjslint-messages-do-not-wrap-function-literals-in-parens-unless-they-are-to-be-immediately-invoked%2F' data-shr_title='JSLint+Messages+-+Do+not+wrap+function+literals+in+parens+unless+they+are+to+be+immediately+invoked.'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F02%2F17%2Fjslint-messages-do-not-wrap-function-literals-in-parens-unless-they-are-to-be-immediately-invoked%2F' data-shr_title='JSLint+Messages+-+Do+not+wrap+function+literals+in+parens+unless+they+are+to+be+immediately+invoked.'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/02/17/jslint-messages-do-not-wrap-function-literals-in-parens-unless-they-are-to-be-immediately-invoked/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JSLint Messages &#8211; Wrap an immediate function invocation in parentheses&#8230;</title>
		<link>http://www.jameswiseman.com/blog/2011/02/17/jslint-messages-wrap-an-immediate-function-invocation-in-parentheses/</link>
		<comments>http://www.jameswiseman.com/blog/2011/02/17/jslint-messages-wrap-an-immediate-function-invocation-in-parentheses/#comments</comments>
		<pubDate>Thu, 17 Feb 2011 13:06:20 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JavaScript-functions]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2201</guid>
		<description><![CDATA[This is the longest error message that JSLint boasts. In full it reads: &#8220;Wrap an immediate function invocation in parentheses to assist the reader in understanding that the expression is the result of a function, and not the function itself.&#8221; What a mouthful! This is relatively simple to explain, however: var a = 1 + [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>This is the longest error message that JSLint boasts. In full it reads:</p>
<p><em>&#8220;Wrap an immediate function invocation in parentheses to assist the reader in understanding that the expression is the result of a function, and not the function itself.&#8221;</em></p>
<p>What a mouthful!</p>
<p>This is relatively simple to explain, however:</p>
<pre name="code" class="JavaScript">var a = 1 + function() { return 1; }(); //assigns '2' to the variable 'a'</pre>
<p>The JSLint error can be addressed by adding parentheses around the function:</p>
<pre name="code" class="JavaScript">var a = 1 + (function() { return 1; })(); //assigns '2' to the variable 'a'</pre>
<p>However, this will yield another JSLint message <a title="Permanet Link to JSLint Messages – Move the invocation into the parens that contain the function" rel="bookmark" href="http://www.jameswiseman.com/blog/2011/02/15/jslint-messages-move-the-invocation-into-the-parens-that-contain-the-function/">Move the invocation into the parens that contain the function</a>.</p>
<p><a title="Permanet Link to JSLint Messages – Move the invocation into the parens that contain the function" rel="bookmark" href="http://www.jameswiseman.com/blog/2011/02/15/jslint-messages-move-the-invocation-into-the-parens-that-contain-the-function/"></a>So, of course to address this, we wrap the whole invocation with the parentheses, like so:</p>
<pre name="code" class="JavaScript">var a = 1 + (function() { return 1; }()); //assigns '2' to the variable 'a'</pre>
<p>So what is the reason for this? Well, actually, the message itself explains is quite well: &#8220;Assist the reader in understanding that the expression is the result of a function, and not the function itself.&#8221; Not all of JSLint is about bugs and problems. Some are all about achieving a consistent style. This is one of them.</p>
<p>This is similar to another wordy JavaScript message: <a title="Function statements are not invocable. Wrap the whole function invocation in parens." href="http://www.jameswiseman.com/blog/2011/02/15/jslint-messages-function-statements-are-not-invocable-wrap-the-entire-immediate-function-invocation-in-parens/">Function statements are not invocable. Wrap the whole function invocation in parens</a>, which captures isolated unwrapped function declarations. Seen in the following example:</p>
<pre name="code" class="JavaScript">var a = 0;
function foo(a) {
    alert(a);
} (a);</pre>
<p>Here, we can see that, again, the invocation is missing a wrapping parentheses. However, in direct contrast to the first example, the code immediately above will fail.</p>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/" target="_blank"> error and warning messages produced by JSLint</a>.</p>
<div class="shr-publisher-2201"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F02%2F17%2Fjslint-messages-wrap-an-immediate-function-invocation-in-parentheses%2F' data-shr_title='JSLint+Messages+-+Wrap+an+immediate+function+invocation+in+parentheses...'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F02%2F17%2Fjslint-messages-wrap-an-immediate-function-invocation-in-parentheses%2F' data-shr_title='JSLint+Messages+-+Wrap+an+immediate+function+invocation+in+parentheses...'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/02/17/jslint-messages-wrap-an-immediate-function-invocation-in-parentheses/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JSLint Messages &#8211; Missing name in function statement</title>
		<link>http://www.jameswiseman.com/blog/2011/02/16/jslint-messages-missing-name-in-function-statement/</link>
		<comments>http://www.jameswiseman.com/blog/2011/02/16/jslint-messages-missing-name-in-function-statement/#comments</comments>
		<pubDate>Wed, 16 Feb 2011 19:44:27 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JavaScript-functions]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2154</guid>
		<description><![CDATA[JSLint reports this error when a function name has been omitted from a functions statement. For example: function(a) { alert(a); } It is possible that the original intention was more like the following: function foo(a) { alert(a); } Although, it is also conceivable that a function expression was also intended: var foo = function(a) { [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>JSLint reports this error when a function name has been omitted from a functions statement. For example:</p>
<pre class="JavaScript" name="code">function(a) {
    alert(a);
}</pre>
<p>It is possible that the original intention was more like the following:</p>
<pre class="JavaScript" name="code">function foo(a) {
    alert(a);
}</pre>
<p>Although, it is also conceivable that a function expression was also intended:</p>
<pre class="JavaScript" name="code">var foo = function(a) {
    alert(a);
}</pre>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/" target="_blank"> error and warning messages produced by JSLint</a>.</p>
<div class="shr-publisher-2154"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F02%2F16%2Fjslint-messages-missing-name-in-function-statement%2F' data-shr_title='JSLint+Messages+-+Missing+name+in+function+statement'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F02%2F16%2Fjslint-messages-missing-name-in-function-statement%2F' data-shr_title='JSLint+Messages+-+Missing+name+in+function+statement'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/02/16/jslint-messages-missing-name-in-function-statement/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JSLint Messages &#8211; Function statements should not be placed in blocks</title>
		<link>http://www.jameswiseman.com/blog/2011/02/16/jslint-messages-function-statements-should-not-be-placed-in-blocks/</link>
		<comments>http://www.jameswiseman.com/blog/2011/02/16/jslint-messages-function-statements-should-not-be-placed-in-blocks/#comments</comments>
		<pubDate>Wed, 16 Feb 2011 13:15:19 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JavaScript-functions]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2160</guid>
		<description><![CDATA[This message was acutally too long for the subject title. In full, it reads: &#8220;Function statements should not be placed in blocks. Use a function expression or move the statement to the top of the outer function.&#8221; We can reproduce this message with the folllowing code: var a = 0; if (a === 0) { function [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>This message was acutally too long for the subject title. In full, it reads:<em> </em></p>
<p><em>&#8220;Function statements should not be placed in blocks. Use a function expression or move the statement to the top of the outer function.&#8221;</em></p>
<p>We can reproduce this message with the folllowing code:</p>
<pre name="code" class="JavaScript">var a = 0;
if (a === 0) {
    function b() {
        alert(1);
    }
} else {
    function b(a) {
        alert(2);
    }
}
b(); //call the function. But which one?</pre>
<p>The intention is to decalre a function based on some condition, but this doesn&#8217;t necessarily work, and the condition is likely to be ignored. Instead we are at the mercy of each inditidual implementation of the JavaScript interpreter.  With the above examples, we may see either &#8220;1&#8243; or &#8220;2&#8243; alerted.</p>
<p>To achieve what we are attempting we need to use a function expression, like so:</p>
<pre name="code" class="JavaScript">var a = 0;
var b;
if (a === 0) {
    b = function() {
        alert(1);
    }
} else {
    b = function(a) {
        alert(2);
    }
}
b(); //call the function.</pre>
<p>This will work every time.</p>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/" target="_blank"> error and warning messages produced by JSLint</a>.</p>
<div class="shr-publisher-2160"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F02%2F16%2Fjslint-messages-function-statements-should-not-be-placed-in-blocks%2F' data-shr_title='JSLint+Messages+-+Function+statements+should+not+be+placed+in+blocks'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F02%2F16%2Fjslint-messages-function-statements-should-not-be-placed-in-blocks%2F' data-shr_title='JSLint+Messages+-+Function+statements+should+not+be+placed+in+blocks'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/02/16/jslint-messages-function-statements-should-not-be-placed-in-blocks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JSLint Messages &#8211; Move the invocation into the parens that contain the function</title>
		<link>http://www.jameswiseman.com/blog/2011/02/15/jslint-messages-move-the-invocation-into-the-parens-that-contain-the-function/</link>
		<comments>http://www.jameswiseman.com/blog/2011/02/15/jslint-messages-move-the-invocation-into-the-parens-that-contain-the-function/#comments</comments>
		<pubDate>Tue, 15 Feb 2011 11:37:43 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JavaScript-closures]]></category>
		<category><![CDATA[JavaScript-functions]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2133</guid>
		<description><![CDATA[With this message we move deep into the heart of JavaScript closures. Without wanting to engage into a discussion about what they or their relative merits are, we can examine how this message might arise. Take the following code: var a = 0; (function(a) { alert(a); })(a); The parentheses around the function are necessary, because when omitted, [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>With this message we move deep into the heart of <a title="JavaScript closures." href="http://jibbering.com/faq/notes/closures/" target="_blank">JavaScript closures</a>. Without wanting to engage into a discussion about what they or their relative merits are, we can examine how this message might arise. Take the following code:</p>
<pre name="code" class="JavaScript">var a = 0;
(function(a) {
    alert(a);
})(a);</pre>
<p>The parentheses around the function are necessary, because when omitted, the compiler assumes it is a Function Statement (see links below).</p>
<p>When referring to &#8216;Invocation&#8217; we actually mean the bit of code <strong>(a)</strong> at the very end of the block of code. Although the code itself will execute perfectly, JSLint prefers that everything is more self-contained, so insists on the following convention instead:</p>
<pre name="code" class="JavaScript">var a = 0;
(function(a) {
    alert(a);
}(a));</pre>
<p>This is almost identical, except the invocation <strong>(a)</strong> now appears within the parentheses that wrap the function.</p>
<p>The most convincing for me is that the initial parentheses isolates the block of code that is being invoked by the second set of parentheses. In addition to this, JSLint has been designed specifically to eliminate conventions and behaviours that have been proven to lead to bugs and issues, so it is possible that sometime in the past the first convention has yielded a bug or some sort of issue.</p>
<p><strong>Some Links</strong></p>
<div>For more information on closures, refer to <a title="Closures in JavaScript" href="http://james.padolsey.com/javascript/closures-in-javascript/" target="_blank">Closures in JavaScript</a> by James Padolsey.</div>
<p>The above function code follows a convention more commonly known as the <a title="A JavaScript Module Pattern" href="http://yuiblog.com/blog/2007/06/12/module-pattern/" target="_blank">JavaScript Module Pattern</a></p>
<p>To demystify the mire of Function Declarations, Function Expressions and Function Statements, refer the article <a title="Named function expressions demystified" href="http://kangax.github.com/nfe/" target="_blank">Named function expressions demystified</a> by Juriy &#8220;kangax&#8221; Zaytsev.</p>
<p>Also thanks to StackOverflow for helping me answer this: <a title="Stackoverflow Question -  Solution for JSLint Errors" href="http://stackoverflow.com/q/1450721/144491" target="_blank">Question &#8211;  Solution for JSLint Errors</a></p>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/" target="_blank"> error and warning messages produced by JSLint</a>.</p>
<div class="shr-publisher-2133"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F02%2F15%2Fjslint-messages-move-the-invocation-into-the-parens-that-contain-the-function%2F' data-shr_title='JSLint+Messages+-+Move+the+invocation+into+the+parens+that+contain+the+function'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F02%2F15%2Fjslint-messages-move-the-invocation-into-the-parens-that-contain-the-function%2F' data-shr_title='JSLint+Messages+-+Move+the+invocation+into+the+parens+that+contain+the+function'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/02/15/jslint-messages-move-the-invocation-into-the-parens-that-contain-the-function/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>JSLint Messages &#8211; Use the array literal notation []</title>
		<link>http://www.jameswiseman.com/blog/2011/02/14/jslint-messages-use-the-array-literal-notation/</link>
		<comments>http://www.jameswiseman.com/blog/2011/02/14/jslint-messages-use-the-array-literal-notation/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 12:00:38 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[array-literal-notation]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2121</guid>
		<description><![CDATA[JSLint has very specific constraints about how you use new. This message is displayed when JSLint encounters an object that has been constructed using new Array() instead of simply = []. In JavaScript we can declare a new array as follows: var a = []; var a = new Array(); This example is innocuous &#8211; they both achieve the [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>JSLint has <a title="JSLint new use constraints" href="http://www.jslint.com/lint.html#new" target="_blank">very specific constraints</a> about how you use new. This message is displayed when JSLint encounters an object that has been constructed using <strong>new Array() </strong>instead of simply <strong>= []</strong>.</p>
<p>In JavaScript we can declare a new array as follows:</p>
<pre class="JavaScript" name="code">    var a = [];
    var a = new Array();</pre>
<p>This example is innocuous &#8211; they both achieve the same effect &#8211; an empty array. But what if we want to add some initialisation?</p>
<pre class="JavaScript" name="code">    var a = [5];  //new array one in size contaning the value 5
    var a = new Array(5);   //new array five in size, each item 'undefined'.
    var a = new Array('5');   //new array one in size containing the value '5'</pre>
<p>Well, here our problems start, as the comments indicate. The first declaration initialises an array with a single value &#8217;5&#8242;, whereas the second declaration initialises an array of five &#8216;undefined&#8217; values. The third declaration, however operates differently to its immediate predecessor, initialising an array with a single value &#8217;5&#8221;.</p>
<p>While they all might be what is intended use of <strong>new Array()</strong> is subject to inconsistency and confusion. In addition to this, once constructed, we have to do a subsequent assignment to add new values. Using the literal notation we can perform the assignment upon declaration.</p>
<p>Furthermore to this, the Array keyword could also be overridden:</p>
<pre class="JavaScript" name="code">    Array = {};
    new Array(); // TypeError: Array is not a constructor</pre>
<p>Whoops! Of course, I would question the wisdom of overriding &#8216;Array&#8217;, but nevertheless, using the array literal notation would help us steer clear of this problematic scenario.</p>
<p>Special thanks to the respondents of Stackoverflow for helping me write this article &#8211; <a title="Stackoverflow Question - JSLint: “Use the array literal notation []” for var os_map = {}" href="http://stackoverflow.com/questions/1936047/jslint-use-the-array-literal-notation-for-var-os-map/1936090#1936090)" target="_blank">Stackoverflow Question &#8211; JSLint: “Use the array literal notation []” for var os_map = {}</a>.</p>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/" target="_blank"> error and warning messages produced by JSLint</a>.</p>
<div class="shr-publisher-2121"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F02%2F14%2Fjslint-messages-use-the-array-literal-notation%2F' data-shr_title='JSLint+Messages+-+Use+the+array+literal+notation+%5B%5D'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F02%2F14%2Fjslint-messages-use-the-array-literal-notation%2F' data-shr_title='JSLint+Messages+-+Use+the+array+literal+notation+%5B%5D'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/02/14/jslint-messages-use-the-array-literal-notation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JSLint Messages &#8211; &#8216;{a}&#8217; is a function.</title>
		<link>http://www.jameswiseman.com/blog/2011/02/01/jslint-messages-a-is-a-function-2/</link>
		<comments>http://www.jameswiseman.com/blog/2011/02/01/jslint-messages-a-is-a-function-2/#comments</comments>
		<pubDate>Tue, 01 Feb 2011 13:37:56 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2070</guid>
		<description><![CDATA[JavaScript functions can be written as a declaration or as expression, as in the following examples: function a(){} //function declaration var a = function(){}; //function expression The first line declares a function, and the second declares a variable. For the purposes of this discussion, we can think of the key word &#8216;function&#8217; as having a [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>JavaScript functions can be written as a declaration or as expression, as in the following examples:</p>
<pre name="code" class="JavaScript">function a(){} //function declaration
var a = function(){};  //function expression</pre>
<p>The first line declares a function, and the second declares a variable. For the purposes of this discussion, we can think of the key word &#8216;function&#8217; as having a similar purpose to the keyword &#8216;var&#8217;, namely, that it declares something (&#8216;a&#8217; in both cases).</p>
<p>Both are perfectly legitimate ways of declaring a function, as is the following:</p>
<pre name="code" class="JavaScript">var a = function(){alert("1";)};
a = function(){alert("2");};</pre>
<p>All we are doing here is reassigning the variable &#8216;a&#8217; with a different function expression. JSLint will not complain about this in the slightest, simply because there is nothing wrong with this. However, what if we substitute the first line above with a function declaration, JSLint starts complaining with <strong>&#8216;a&#8217; is a function</strong>:</p>
<pre name="code" class="JavaScript">function a(){alert("1";)}; //JSLint error: <strong>'a' is a function</strong>
a = function(){alert("2");};</pre>
<p>Instead of &#8216;a&#8217; being a &#8216;var&#8217;, it is a &#8216;function&#8217;, but in the second statement we attempt to use it as a &#8216;var&#8217;. In other words we are attempting to turn it from a function declaration to a  function expression.</p>
<p>You may find out that the interpreter is tolerant of this, but it is still indicative of something potentially problematic, and possibly not what was intended.</p>
<p>For more information on function declarations and function expressions, <a title="JavaScript, JavaScript" rel="home" href="http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/" target="_blank">refer to this article at JavaScript, JavaScript</a>. Related, and also of interest is this article on <a title="JavaScript Scoping and Hoisting" href="http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting" target="_blank">JavaScript Scoping and Hoisting</a>.</p>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/" target="_blank"> error and warning messages produced by JSLint</a>.</p>
<div class="shr-publisher-2070"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F02%2F01%2Fjslint-messages-a-is-a-function-2%2F' data-shr_title='JSLint+Messages+-+%27%7Ba%7D%27+is+a+function.'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F02%2F01%2Fjslint-messages-a-is-a-function-2%2F' data-shr_title='JSLint+Messages+-+%27%7Ba%7D%27+is+a+function.'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/02/01/jslint-messages-a-is-a-function-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JSLint Messages &#8211; Bad Assignment</title>
		<link>http://www.jameswiseman.com/blog/2011/01/30/jslint-messages-bad-assignment/</link>
		<comments>http://www.jameswiseman.com/blog/2011/01/30/jslint-messages-bad-assignment/#comments</comments>
		<pubDate>Sun, 30 Jan 2011 19:54:52 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2052</guid>
		<description><![CDATA[JSLint prompts you with this message when it encounters what it perceives to be a dangerous, or &#8220;bad&#8221; assignment. For example: document.getElementById("text"+x) = function(){}; In the above code, we are calling a function, and then attempting to assign it a value. Clearly the case of document.getElementById() nonsense anyway, and will fail. It doesn&#8217;t shed any [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>JSLint prompts you with this message when it encounters what it perceives to be a dangerous, or &#8220;bad&#8221; assignment. For example:</p>
<pre class="JavaScript" name="code">document.getElementById("text"+x) = function(){};</pre>
<p>In the above code, we are calling a function, and then attempting to assign it a value. Clearly the case of <strong>document.getElementById()</strong> nonsense anyway, and will fail. It doesn&#8217;t shed any light as to how this sort of error might occur, so let us look an example with custom objects.</p>
<p>It is possible to create a function within a JavaScript object in many ways. The following demonstrates one such scenario:</p>
<pre class="JavaScript" name="code">var obj = { fn : function () {}};</pre>
<p>No problems here. But what if we wanted an ad-hoc creation (possibly as part of some sort of desire to extend an existing object)? We might see something resembling the following:</p>
<pre class="JavaScript" name="code">var obj2 = {};
obj2.fn = function () {};</pre>
<p>Again, no problem. However it is conceivable that we might encounter the following (entered, perhaps, by accident):</p>
<pre class="JavaScript" name="code">var obj2 = {};
obj2.fn() = function () {};</pre>
<p>Do you spot the subtle difference? The developer has stipulated an extra set of brackets. It will certainly not what was intended, but has found its way into the code. When JSLint encounters this, will complain about a bad assignment.</p>
<p>But, in JavaScript, object members can also be referenced using the array/collection [] notation. Happily, JSLint is clever enough to spot this. So the following will yield the same error (along with another JSLint error <a title="[{a}] is better written in dot notation." href="http://www.jameswiseman.com/blog/2011/01/17/jslint-messages-expression-is-better-written-in-dot-notation/" target="_blank">[{a}] is better written in dot notation</a>):</p>
<pre class="JavaScript" name="code">document["getElementById"]("test") = function(){};</pre>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/" target="_blank"> error and warning messages produced by JSLint</a>.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><img class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/pixy.gif?x-id=0da1e6c0-a538-405c-b1e1-8444ce142cd3" alt="" /><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<div class="shr-publisher-2052"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F01%2F30%2Fjslint-messages-bad-assignment%2F' data-shr_title='JSLint+Messages+-+Bad+Assignment'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F01%2F30%2Fjslint-messages-bad-assignment%2F' data-shr_title='JSLint+Messages+-+Bad+Assignment'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/01/30/jslint-messages-bad-assignment/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JSLint Messages &#8211; Don&#8217;t make functions within a loop.</title>
		<link>http://www.jameswiseman.com/blog/2011/01/27/jslint-messages-dont-make-functions-within-a-loop/</link>
		<comments>http://www.jameswiseman.com/blog/2011/01/27/jslint-messages-dont-make-functions-within-a-loop/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 17:02:28 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2049</guid>
		<description><![CDATA[In JavaScript we can declare a function anonymously, and using a concept known as late-binding we can attach the event to an HTML object: document.getElementById("control").onclick = function(){//code}; We can also conceive a situation whereby we can do this to multiple controls within a loop. Casting aside the capabilities of JavaScript libraries such as jQuery for the [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>In JavaScript we can <a title="JavaScript Anonymous Functions" href="http://helephant.com/2008/08/javascript-anonymous-functions/" target="_blank">declare a function anonymously</a>, and using a concept known as <a title="A List Apart - Late Binding" href="http://www.alistapart.com/articles/getoutbindingsituations/" target="_blank">late-binding</a> we can attach the event to an HTML object:</p>
<pre name="code" class="JavaScript">document.getElementById("control").onclick = function(){//code};</pre>
<p>We can also conceive a situation whereby we can do this to multiple controls within a loop. Casting aside the capabilities of JavaScript libraries such as jQuery for the moment (we&#8217;ll examine jQuery  later), the code might look something like:</p>
<pre name="code" class="JavaScript">var x=0;
for(x = 1; x&lt;4; x++){
    document.getElementById("text"+x).onclick = function(){
        alert("test");
    };
}</pre>
<p>Herein lies the complaint of JSLint: &#8220;Don&#8217;t make functions within a loop.&#8221; In the above code, on every iteration of the loop we will evaluate the function. This is wasteful and unnecessary. It&#8217;s much better simply to store the function in a variable:</p>
<pre name="code" class="JavaScript">var x=0;
var f = function(){
    alert("test");
};

for(x = 1; x&lt;4; x++){
    document.getElementById("but"+x).onclick = f;
}</pre>
<p>For many cases this will be sufficient,  but the above ignores the frequent requirement to pass values to functions. So how can we do this?</p>
<p>Well, in the above example a parameter <em>will</em> be passed to the function automatically. This parameter will be the event object that represents the source event. From this object we can infer the event source (e.g. an HTML button), and from the event source, we can infer object data.</p>
<p>Consider the following code (See it work <a title="JavaScript Events and Anonymous Functions JSFiddle" href="http://jsfiddle.net/MpBE3/" target="_blank">at this JSFiddle</a>):</p>
<pre name="code" class="JavaScript">var x=0;
var f = function(evt){
    evt.target = evt.target ? evt.target : evt.srcElement;  //cross-browser event target retrieval
    alert(evt.target.getAttribute("data-info"));
};

for(x = 1; x&lt;=3; x++){
    var objButton = document.getElementById("but"+x);
    objButton.setAttribute("data-info", x);  //set a 'data-'attribute
    objButton.onclick = f;
}</pre>
<p>Here we bind the data (<strong>x</strong>) to the control using the <strong>setAttribute()</strong> function on the object. We can retrieve this by calling <strong>getAttribute</strong> on the event target when the event fires. Refer to the <a title="JavaScript kit event object" href="http://www.javascriptkit.com/jsref/event.shtml" target="_blank">JavaScript kit event object</a> for more information on JavaScript events.</p>
<p>Or, of course, we can disregard the event obejct altogther, and instead use the JavaScript <strong>this</strong> object (available at this <a title="JSFiddle" href="http://jsfiddle.net/MpBE3/1/" target="_blank">slightly updated fiddle</a>)</p>
<pre name="code" class="JavaScript">var x=0;
var f = function(evt){
    alert(this.getAttribute("data-info"));
};

for(x = 1; x&lt;=3; x++){
    var objButton = document.getElementById("but"+x);
    objButton.setAttribute("data-info", x);
    objButton.onclick = f;
}</pre>
<p>The <a title="quirksmode.org JavaScript this" href="http://www.quirksmode.org/js/this.html" target="_blank">JavaScript <strong>this </strong>object</a> is discussed in more depth over at QuirksMode.org.</p>
<p>And what about jQuery? Well, this can actually hide this sort of problem from JSLint. Consider the following:</p>
<pre name="code" class="JavaScript">$("input:button").each(function () {
    $(this).click(function () {});
});</pre>
<p>This slightly contrived example (contrived because we don&#8217;t need to call the <strong>.each()</strong> to bind for all elements in the collection) will pass a JSLint scan, and yet, still break the rule that it was trying to preserve. We are still evaluating a function for every call of <strong>.each()</strong>, but JSLint doesn&#8217;t (and can&#8217;t) know this <strong>.each()</strong> functions as a loop.</p>
<p>But, adopting the pattern above in the same manner will solve this problem.</p>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/" target="_blank"> error and warning messages produced by JSLint</a>.</p>
<div class="shr-publisher-2049"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F01%2F27%2Fjslint-messages-dont-make-functions-within-a-loop%2F' data-shr_title='JSLint+Messages+-+Don%27t+make+functions+within+a+loop.'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F01%2F27%2Fjslint-messages-dont-make-functions-within-a-loop%2F' data-shr_title='JSLint+Messages+-+Don%27t+make+functions+within+a+loop.'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/01/27/jslint-messages-dont-make-functions-within-a-loop/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JSLint Messages &#8211; Expected to see a statement and instead saw a block.</title>
		<link>http://www.jameswiseman.com/blog/2011/01/26/jslint-messages-expected-to-see-a-statement-and-instead-saw-a-block/</link>
		<comments>http://www.jameswiseman.com/blog/2011/01/26/jslint-messages-expected-to-see-a-statement-and-instead-saw-a-block/#comments</comments>
		<pubDate>Wed, 26 Jan 2011 14:49:19 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[scoping]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2034</guid>
		<description><![CDATA[A programming convention that is frequently carried into JavaScript from other languages (often by C++ and C developers)  is the scoping of functionality within isolated blocks of code (i.e. between parentheses). Take the following: int y = 2; { int x = 3+y; DoSomethingWith(x); } But JavaScript is less fussy about scoping, so the following will [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>A programming convention that is frequently carried into JavaScript from other languages (often by C++ and C developers)  is the scoping of functionality within isolated blocks of code (i.e. between parentheses).</p>
<p>Take the following:</p>
<pre class="JavaScript" name="code">int y = 2;
{
    int x = 3+y;
    DoSomethingWith(x);
}</pre>
<p>But JavaScript is less fussy about scoping, so the following will execute probably execute without complaint:</p>
<pre class="JavaScript" name="code">var y = 2;
{
   var x = 3;
}
alert(x+y); //alerts '5'</pre>
<p>The use of the block here, therefore, is just extra noise. Its more characters to be written, read and transmitted. In addition to this, in some cases its contrary to the language specification. Consider the use of the &#8216;case&#8217; statement:</p>
<pre class="JavaScript" name="code">var x = 0;
switch(x){
    case 0:{
            var y = 1;
            alert(y);
        }
}</pre>
<p>Here we have added some parentheses to the &#8216;case&#8217; block of code. In C, C++, <a title="C++ Case Parens" href="http://stackoverflow.com/q/92396/144491" target="_blank">this sort of convention is necessary</a>. In JavaScript, it is not, so the following will work happily:</p>
<pre class="JavaScript" name="code">var x = 0;
switch(x){
    case 0:
        var y = 1;
        alert(y);
}</pre>
<p>The first statement is contrary to the language specification. (Refer to the <a title="JavaScript EC5 Specification" href="http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf" target="_blank">official EC5 Specification</a> and the <a title="Mozilla JavaScript reference" href="https://developer.mozilla.org/en/JavaScript/Reference/Statements/switch" target="_blank">Mozilla JavaScript reference</a> for more information). This reason alone is justification enough for JSLint to throw the error &#8216;Expected to see a statement and instead saw a block&#8217;, even before considering the extra noise the blocks create.</p>
<p>There is another potential argument against the use of (bogus) scoping blocks in this way. JavaScript also uses parentheses for objects and object literal declarations:</p>
<pre class="JavaScript" name="code">var y = {
    x:3,
    f:2
};</pre>
<p>So, avoiding the specification of bogus scoped blocks will also make our code easier to read and subject to less confusion.</p>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/" target="_blank"> error and warning messages produced by JSLint</a>.</p>
<div class="shr-publisher-2034"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F01%2F26%2Fjslint-messages-expected-to-see-a-statement-and-instead-saw-a-block%2F' data-shr_title='JSLint+Messages+-+Expected+to+see+a+statement+and+instead+saw+a+block.'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F01%2F26%2Fjslint-messages-expected-to-see-a-statement-and-instead-saw-a-block%2F' data-shr_title='JSLint+Messages+-+Expected+to+see+a+statement+and+instead+saw+a+block.'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/01/26/jslint-messages-expected-to-see-a-statement-and-instead-saw-a-block/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JSLint Messages &#8211; A leading decimal point can be confused with a dot &#8216;.{a}&#8217;</title>
		<link>http://www.jameswiseman.com/blog/2011/01/25/jslint-messages-a-leading-decimal-point-can-be-confused-with-a-dot-a/</link>
		<comments>http://www.jameswiseman.com/blog/2011/01/25/jslint-messages-a-leading-decimal-point-can-be-confused-with-a-dot-a/#comments</comments>
		<pubDate>Tue, 25 Jan 2011 16:50:14 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2024</guid>
		<description><![CDATA[With this message, JSLint is warning you of a potentially hazardous use of the decimal point. The following code will yield this error: alert(.2); Here. JSLint is picking out conventions that may be indicitive of some other problem in your code. It is also not making the assumption that every single JavaScript interpreter will handle [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>With this message, JSLint is warning you of a potentially hazardous use of the decimal point. The following code will yield this error:</p>
<pre class="JavaScript" name="code">alert(.2);</pre>
<p>Here. JSLint is picking out conventions that may be indicitive of some other problem in your code. It is also not making the assumption that every single JavaScript interpreter will handle this code correctly.</p>
<p>This is closely related to the message <a title="A leading decimal point can be confused with a dot '.{a}'" href="http://www.jameswiseman.com/blog/?p=2010" target="_blank">A trailing decimal point can be confused with a dot &#8216;{a}&#8217;</a></p>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/" target="_blank"> error and warning messages produced by JSLint</a>.</p>
<div class="shr-publisher-2024"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F01%2F25%2Fjslint-messages-a-leading-decimal-point-can-be-confused-with-a-dot-a%2F' data-shr_title='JSLint+Messages+-+A+leading+decimal+point+can+be+confused+with+a+dot+%27.%7Ba%7D%27'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F01%2F25%2Fjslint-messages-a-leading-decimal-point-can-be-confused-with-a-dot-a%2F' data-shr_title='JSLint+Messages+-+A+leading+decimal+point+can+be+confused+with+a+dot+%27.%7Ba%7D%27'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/01/25/jslint-messages-a-leading-decimal-point-can-be-confused-with-a-dot-a/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JSLint Messages &#8211; A trailing decimal point can be confused with a dot &#8216;{a}&#8217;.</title>
		<link>http://www.jameswiseman.com/blog/2011/01/25/jslint-messages-a-trailing-decimal-point-can-be-confused-with-a-dot-a/</link>
		<comments>http://www.jameswiseman.com/blog/2011/01/25/jslint-messages-a-trailing-decimal-point-can-be-confused-with-a-dot-a/#comments</comments>
		<pubDate>Tue, 25 Jan 2011 16:50:09 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2010</guid>
		<description><![CDATA[I kept stumbling across this one when investigating the message A dot following a number can be confused with a decimal point. This particular message proved easier to produce than the aforementioned one. With this message, JSLint is warning you of a potentially hazardous use of the decimal point. The following code will yield this error: [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I kept stumbling across this one when investigating the message <a title="A dot following a number can be confused with a decimal point." href="http://www.jameswiseman.com/blog/2011/01/24/jslint-messages-a-dot-following-a-number-can-be-confused-with-a-decimal-point/" target="_blank">A dot following a number can be confused with a decimal point.</a> This particular message proved easier to produce than the aforementioned one.</p>
<p>With this message, JSLint is warning you of a potentially hazardous use of the decimal point. The following code will yield this error:</p>
<pre class="JavaScript" name="code">alert(2.);</pre>
<p>Here. JSLint is picking out conventions that may be indicitive of some other problem in your code. It is also not making the assumption that every single JavaScript interpreter will handle this code correctly.</p>
<p>This is closely related to the message <a title="A leading decimal point can be confused with a dot '.{a}'" href="http://www.jameswiseman.com/blog/?p=2024" target="_blank">A leading decimal point can be confused with a dot &#8216;.{a}&#8217;</a></p>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/" target="_blank"> error and warning messages produced by JSLint</a>.</p>
<div class="shr-publisher-2010"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F01%2F25%2Fjslint-messages-a-trailing-decimal-point-can-be-confused-with-a-dot-a%2F' data-shr_title='JSLint+Messages+-+A+trailing+decimal+point+can+be+confused+with+a+dot+%27%7Ba%7D%27.'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F01%2F25%2Fjslint-messages-a-trailing-decimal-point-can-be-confused-with-a-dot-a%2F' data-shr_title='JSLint+Messages+-+A+trailing+decimal+point+can+be+confused+with+a+dot+%27%7Ba%7D%27.'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/01/25/jslint-messages-a-trailing-decimal-point-can-be-confused-with-a-dot-a/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JSLint Messages &#8211; A dot following a number can be confused with a decimal point.</title>
		<link>http://www.jameswiseman.com/blog/2011/01/24/jslint-messages-a-dot-following-a-number-can-be-confused-with-a-decimal-point/</link>
		<comments>http://www.jameswiseman.com/blog/2011/01/24/jslint-messages-a-dot-following-a-number-can-be-confused-with-a-decimal-point/#comments</comments>
		<pubDate>Mon, 24 Jan 2011 19:25:00 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=1984</guid>
		<description><![CDATA[This is a strange one, and took  some tracking down. Most attempts to bring about this message merely yielded another similar message: A trailing decimal point can be confused with a dot &#8216;{a}&#8217;. Eventually I stumbled across it. So, what do you expect the following to yield?  (Readers should note that the space between the &#8217;2&#8242; [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>This is a strange one, and took  some tracking down. Most attempts to bring about this message merely yielded another similar message: <a title="A trailing decimal point can be confused with a dot '{a}'." href="http://www.jameswiseman.com/blog/?p=2010" target="_blank">A trailing decimal point can be confused with a dot &#8216;{a}&#8217;.</a></p>
<p>Eventually I stumbled across it. So, what do you expect the following to yield?  (Readers should note that the space between the &#8217;2&#8242;  and the &#8216;.x&#8217; is intentional):</p>
<pre name="code" class="JavaScript">alert(2 .x);</pre>
<p>Well, the answer is &#8216;undefined&#8217;.</p>
<p>Eh?</p>
<p>Things only appear to get stranger. Try this:</p>
<pre name="code" class="JavaScript">alert(2 .valueOf());</pre>
<p>The alert here pops up with &#8217;2&#8242;. Okay, so clearly it&#8217;s another way of writing the following&#8230;</p>
<pre name="code" class="JavaScript">alert(2.valueOf());</pre>
<p>Right? Wrong! This merely yields a syntax error.</p>
<p>What&#8217;s going on here is best explained over at Stackoverflow: <a title="What's going on with this rather peculiar JavaScript syntax" href="http://stackoverflow.com/q/4783811/144491" target="_blank">What&#8217;s going on with this rather peculiar JavaScript syntax?</a></p>
<blockquote><p>A floating-point numeric constant must not have embedded spaces. Thus, &#8217;2 .x&#8217; is an expression calling for the constant &#8217;2&#8242; to be promoted to a Number object, and then the property called &#8216;x&#8217; is examined. There isn&#8217;t one, of course, so the value is &#8217;undefined&#8217;.</p></blockquote>
<p>I can see why JSLint complains about it. The convention is so arcane that it&#8217;s probably not what the developer intended, and if they did then they really ought to have known better!</p>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/" target="_blank"> error and warning messages produced by JSLint</a>.</p>
<div>
<p><span style="font-family: monospace;"><span style="line-height: normal; white-space: pre-wrap;"> </span></span></p>
</div>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><img class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/pixy.gif?x-id=e69deb7c-6f5e-41c6-8039-94f374a425bc" alt="" /><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<div class="shr-publisher-1984"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F01%2F24%2Fjslint-messages-a-dot-following-a-number-can-be-confused-with-a-decimal-point%2F' data-shr_title='JSLint+Messages+-+A+dot+following+a+number+can+be+confused+with+a+decimal+point.'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F01%2F24%2Fjslint-messages-a-dot-following-a-number-can-be-confused-with-a-decimal-point%2F' data-shr_title='JSLint+Messages+-+A+dot+following+a+number+can+be+confused+with+a+decimal+point.'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/01/24/jslint-messages-a-dot-following-a-number-can-be-confused-with-a-decimal-point/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JSLint Messages &#8211; Expected a conditional expression and instead saw an assignment</title>
		<link>http://www.jameswiseman.com/blog/2011/01/24/jslint-messages-expected-a-conditional-expression-and-instead-saw-an-assignment/</link>
		<comments>http://www.jameswiseman.com/blog/2011/01/24/jslint-messages-expected-a-conditional-expression-and-instead-saw-an-assignment/#comments</comments>
		<pubDate>Mon, 24 Jan 2011 13:18:00 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[jslint-messages]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=2001</guid>
		<description><![CDATA[It could be argued that this is one of the family of messages under Expected ‘{a}’ and instead saw ‘{b}’. One of my university professor referred to as &#8216;error 1&#8242;, because it was the single most common and hard-to-spot syntax issue he encountered. Although he was a C programmer by trade, the syntactic similarities between C and [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>It could be argued that this is one of the family of messages under <a title="Expected ‘{a}’ and instead saw ‘{b}’" href="http://www.jameswiseman.com/blog/2011/01/18/jslint-messages-expected-a-and-instead-saw-b/" target="_blank">Expected ‘{a}’ and instead saw ‘{b}’</a>. One of my university professor referred to as &#8216;error 1&#8242;, because it was the single most common and hard-to-spot syntax issue he encountered. Although he was a C programmer by trade, the syntactic similarities between C and JavaScript have allowed the issue to exist in both languages.</p>
<p>Here is some problem code:</p>
<pre name="code" class="JavaScript">if (x=0){
    //do stuff
}</pre>
<p>What it is, of course, missing is a comparison operator. We have, instead, the <em>assignment operator</em> &#8216;=&#8217; where would really be expecting an <em>equality operator</em> &#8216;==&#8217;, or, even better an <em>identity operator</em> &#8216;===&#8217;. We can see it in action in<a title="Operators JSFiddle" href="http://jsfiddle.net/RxKqw/1/" target="_blank"> this JSFiddle</a>.</p>
<p>The above code will return &#8217;0&#8242; which resolves to &#8216;false&#8217;, and therefore the block of code is not executed. Where it is non-zero it is assumed to be true, and the code is executed. Regardless, the statement is broken, or indicative of some very poor code.</p>
<p>Refer to <a title="JavaScript operators" onclick="javascript:_gaq.push(['_trackEvent','outbound-article','www.daaq.net']);" href="http://www.daaq.net/old/javascript/index.php?page=js+operators&amp;parent=core+javascript" target="_blank">this article on JavaScript operators</a> for more information on:</p>
<ul>
<li>arithmetic operators,</li>
<li>relational operators,</li>
<li>string operators,</li>
<li>logical operators,</li>
<li>bitwise operators, and</li>
<li>a few others that don’t really fit into any category.</li>
</ul>
<p><strong>A Guide To JSLint Messages</strong></p>
<p>This article is one of a series on the<a title="Error and warning messages produced by JSLint" href="http://www.jameswiseman.com/blog/2011/01/17/jslint-a-guide-to-jslint-messages/" target="_blank"> error and warning messages produced by JSLint</a>.</p>
<div class="shr-publisher-2001"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F01%2F24%2Fjslint-messages-expected-a-conditional-expression-and-instead-saw-an-assignment%2F' data-shr_title='JSLint+Messages+-+Expected+a+conditional+expression+and+instead+saw+an+assignment'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2011%2F01%2F24%2Fjslint-messages-expected-a-conditional-expression-and-instead-saw-an-assignment%2F' data-shr_title='JSLint+Messages+-+Expected+a+conditional+expression+and+instead+saw+an+assignment'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2011/01/24/jslint-messages-expected-a-conditional-expression-and-instead-saw-an-assignment/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	<img style='margin:0;padding:0;border:0;' width='1px' height='1px' src="http://www.jameswiseman.com/blog/wp-content/plugins/mystat/mystat.php?act=time_load&id=623886&rnd=1122934762" /></channel>
</rss>

