<?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 &#8211; Creating Default Arguments</title>
	<atom:link href="http://www.fettesps.com/javascript-creating-default-arguments/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fettesps.com/javascript-creating-default-arguments/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=javascript-creating-default-arguments</link>
	<description>Fettes Programming Solutions</description>
	<lastBuildDate>Sun, 05 Feb 2012 08:57:22 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Maat</title>
		<link>http://www.fettesps.com/javascript-creating-default-arguments/comment-page-1/#comment-356</link>
		<dc:creator>Maat</dc:creator>
		<pubDate>Fri, 12 Jun 2009 08:12:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.fettesps.com/?p=221#comment-356</guid>
		<description>IMO it is more readable/understandable to use the &quot;arguments&quot; variable and it allows more flexibility. Something  like:

function lorum(/*x, y*/) {
    var x;
    var y;
    switch(arguments.length) {
        case 0 :
            x = 100;
            y = 500;
            break
        case 1 :
            x = arguments[0];
            y = 500;
            break
        case 2 :
            x = arguments[0];
            y = arguments[1];
            break;
    }
    // ...
}</description>
		<content:encoded><![CDATA[<p>IMO it is more readable/understandable to use the &#8220;arguments&#8221; variable and it allows more flexibility. Something  like:</p>
<p>function lorum(/*x, y*/) {<br />
    var x;<br />
    var y;<br />
    switch(arguments.length) {<br />
        case 0 :<br />
            x = 100;<br />
            y = 500;<br />
            break<br />
        case 1 :<br />
            x = arguments[0];<br />
            y = 500;<br />
            break<br />
        case 2 :<br />
            x = arguments[0];<br />
            y = arguments[1];<br />
            break;<br />
    }<br />
    // &#8230;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: k4st</title>
		<link>http://www.fettesps.com/javascript-creating-default-arguments/comment-page-1/#comment-333</link>
		<dc:creator>k4st</dc:creator>
		<pubDate>Tue, 09 Jun 2009 22:00:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.fettesps.com/?p=221#comment-333</guid>
		<description>Ah, very good point; I hadn&#039;t thought of that. Thank you.</description>
		<content:encoded><![CDATA[<p>Ah, very good point; I hadn&#8217;t thought of that. Thank you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: geekfreak</title>
		<link>http://www.fettesps.com/javascript-creating-default-arguments/comment-page-1/#comment-336</link>
		<dc:creator>geekfreak</dc:creator>
		<pubDate>Tue, 09 Jun 2009 18:37:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.fettesps.com/?p=221#comment-336</guid>
		<description>oops...

function flag(flag){
return flag &#124;&#124; true;
}

flag(true) == flag(false) == flag();</description>
		<content:encoded><![CDATA[<p>oops&#8230;</p>
<p>function flag(flag){<br />
return flag || true;<br />
}</p>
<p>flag(true) == flag(false) == flag();</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: geekfreak</title>
		<link>http://www.fettesps.com/javascript-creating-default-arguments/comment-page-1/#comment-334</link>
		<dc:creator>geekfreak</dc:creator>
		<pubDate>Tue, 09 Jun 2009 18:11:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.fettesps.com/?p=221#comment-334</guid>
		<description>The typeof === undefined comparision is better as it will not cause problems with boolean (or falsey) parameter values.

function foo(flag){
return flag &#124;&#124; true;
}

flag(true) == flag(false) == flag();


a corner case for sure, but certainly a failure case for the boolean operand strategy</description>
		<content:encoded><![CDATA[<p>The typeof === undefined comparision is better as it will not cause problems with boolean (or falsey) parameter values.</p>
<p>function foo(flag){<br />
return flag || true;<br />
}</p>
<p>flag(true) == flag(false) == flag();</p>
<p>a corner case for sure, but certainly a failure case for the boolean operand strategy</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cancel bubble</title>
		<link>http://www.fettesps.com/javascript-creating-default-arguments/comment-page-1/#comment-330</link>
		<dc:creator>cancel bubble</dc:creator>
		<pubDate>Tue, 09 Jun 2009 15:03:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.fettesps.com/?p=221#comment-330</guid>
		<description>You should be declaring your variables with var inside the function, otherwise you&#039;re creating global variables and are much more likely to have name collisions.  vars inside a function are &quot;private&quot;, a variable declared within a function is not accessible outside of that function.

function ipsum(i, j) {
    var i = i &#124;&#124; 100;
    var j = j &#124;&#124; 500;

    // or in one var declaration

    var i = i &#124;&#124; 100, j = j &#124;&#124; 500
}</description>
		<content:encoded><![CDATA[<p>You should be declaring your variables with var inside the function, otherwise you&#8217;re creating global variables and are much more likely to have name collisions.  vars inside a function are &#8220;private&#8221;, a variable declared within a function is not accessible outside of that function.</p>
<p>function ipsum(i, j) {<br />
    var i = i || 100;<br />
    var j = j || 500;</p>
<p>    // or in one var declaration</p>
<p>    var i = i || 100, j = j || 500<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: contantofaz</title>
		<link>http://www.fettesps.com/javascript-creating-default-arguments/comment-page-1/#comment-324</link>
		<dc:creator>contantofaz</dc:creator>
		<pubDate>Tue, 09 Jun 2009 14:57:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.fettesps.com/?p=221#comment-324</guid>
		<description>If i or j is meant to be set to 0 by the end-user, this code will not allow that:

    function tell_it(i, j){
        i = i === undefined ? 100 : i;
        j = j === undefined ? 500 : j;
        system.k7.shell.print(i);
        system.k7.shell.print(j);
        }
    
    function say_what(i, j){
        i = i &#124;&#124; 100;
        j = j &#124;&#124; 500;
        system.k7.shell.print(i);
        system.k7.shell.print(j);
        }
        
    system.k7.shell.print(&#039;----- say what&#039;);
    
    say_what();
    say_what(1, 2);
    say_what(0, 3);
    
    system.k7.shell.print(&#039;----- tell it&#039;);
    
    tell_it();
    tell_it(1, 2);
    tell_it(0, 3);
    
    
    
    Outputs:
    
    $ k7 js_is_cool.js
    ----- say what
    100
    500
    1
    2
    100
    3
    ----- tell it
    100
    500
    1
    2
    0
    3
</description>
		<content:encoded><![CDATA[<p>If i or j is meant to be set to 0 by the end-user, this code will not allow that:</p>
<p>    function tell_it(i, j){<br />
        i = i === undefined ? 100 : i;<br />
        j = j === undefined ? 500 : j;<br />
        system.k7.shell.print(i);<br />
        system.k7.shell.print(j);<br />
        }</p>
<p>    function say_what(i, j){<br />
        i = i || 100;<br />
        j = j || 500;<br />
        system.k7.shell.print(i);<br />
        system.k7.shell.print(j);<br />
        }</p>
<p>    system.k7.shell.print(&#8216;&#8212;&#8211; say what&#8217;);</p>
<p>    say_what();<br />
    say_what(1, 2);<br />
    say_what(0, 3);</p>
<p>    system.k7.shell.print(&#8216;&#8212;&#8211; tell it&#8217;);</p>
<p>    tell_it();<br />
    tell_it(1, 2);<br />
    tell_it(0, 3);</p>
<p>    Outputs:</p>
<p>    $ k7 js_is_cool.js<br />
    &#8212;&#8211; say what<br />
    100<br />
    500<br />
    1<br />
    2<br />
    100<br />
    3<br />
    &#8212;&#8211; tell it<br />
    100<br />
    500<br />
    1<br />
    2<br />
    0<br />
    3</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: contantofaz</title>
		<link>http://www.fettesps.com/javascript-creating-default-arguments/comment-page-1/#comment-325</link>
		<dc:creator>contantofaz</dc:creator>
		<pubDate>Tue, 09 Jun 2009 14:57:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.fettesps.com/?p=221#comment-325</guid>
		<description>If i or j is meant to be set to 0 by the end-user, this code will not allow that:

    function tell_it(i, j){
        i = i === undefined ? 100 : i;
        j = j === undefined ? 500 : j;
        system.k7.shell.print(i);
        system.k7.shell.print(j);
        }
    
    function say_what(i, j){
        i = i &#124;&#124; 100;
        j = j &#124;&#124; 500;
        system.k7.shell.print(i);
        system.k7.shell.print(j);
        }
        
    system.k7.shell.print(&#039;----- say what&#039;);
    
    say_what();
    say_what(1, 2);
    say_what(0, 3);
    
    system.k7.shell.print(&#039;----- tell it&#039;);
    
    tell_it();
    tell_it(1, 2);
    tell_it(0, 3);
    
    
    
    Outputs:
    
    $ k7 js_is_cool.js
    ----- say what
    100
    500
    1
    2
    100
    3
    ----- tell it
    100
    500
    1
    2
    0
    3
</description>
		<content:encoded><![CDATA[<p>If i or j is meant to be set to 0 by the end-user, this code will not allow that:</p>
<p>    function tell_it(i, j){<br />
        i = i === undefined ? 100 : i;<br />
        j = j === undefined ? 500 : j;<br />
        system.k7.shell.print(i);<br />
        system.k7.shell.print(j);<br />
        }</p>
<p>    function say_what(i, j){<br />
        i = i || 100;<br />
        j = j || 500;<br />
        system.k7.shell.print(i);<br />
        system.k7.shell.print(j);<br />
        }</p>
<p>    system.k7.shell.print(&#8216;&#8212;&#8211; say what&#8217;);</p>
<p>    say_what();<br />
    say_what(1, 2);<br />
    say_what(0, 3);</p>
<p>    system.k7.shell.print(&#8216;&#8212;&#8211; tell it&#8217;);</p>
<p>    tell_it();<br />
    tell_it(1, 2);<br />
    tell_it(0, 3);</p>
<p>    Outputs:</p>
<p>    $ k7 js_is_cool.js<br />
    &#8212;&#8211; say what<br />
    100<br />
    500<br />
    1<br />
    2<br />
    100<br />
    3<br />
    &#8212;&#8211; tell it<br />
    100<br />
    500<br />
    1<br />
    2<br />
    0<br />
    3</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: k4st</title>
		<link>http://www.fettesps.com/javascript-creating-default-arguments/comment-page-1/#comment-322</link>
		<dc:creator>k4st</dc:creator>
		<pubDate>Tue, 09 Jun 2009 10:19:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.fettesps.com/?p=221#comment-322</guid>
		<description>In fact, you can shorten that in Javascript to:

    i = i &#124;&#124; 100;
    j = j &#124;&#124; 500;</description>
		<content:encoded><![CDATA[<p>In fact, you can shorten that in Javascript to:</p>
<p>    i = i || 100;<br />
    j = j || 500;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: contantofaz</title>
		<link>http://www.fettesps.com/javascript-creating-default-arguments/comment-page-1/#comment-318</link>
		<dc:creator>contantofaz</dc:creator>
		<pubDate>Tue, 09 Jun 2009 07:24:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.fettesps.com/?p=221#comment-318</guid>
		<description>What are the drawbacks to checking whether the variable exists more directly with:

    function ipsum(i, j) {
     
        i = i === undefined ? 100 : i;
        j = j === undefined ? 500 : j;
     
        // ...
    }    </description>
		<content:encoded><![CDATA[<p>What are the drawbacks to checking whether the variable exists more directly with:</p>
<p>    function ipsum(i, j) {</p>
<p>        i = i === undefined ? 100 : i;<br />
        j = j === undefined ? 500 : j;</p>
<p>        // &#8230;<br />
    }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Goodman</title>
		<link>http://www.fettesps.com/javascript-creating-default-arguments/comment-page-1/#comment-321</link>
		<dc:creator>Peter Goodman</dc:creator>
		<pubDate>Tue, 09 Jun 2009 05:19:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.fettesps.com/?p=221#comment-321</guid>
		<description>Or even shorter:

i = i &#124;&#124; 100;
j = j &#124;&#124; 500;</description>
		<content:encoded><![CDATA[<p>Or even shorter:</p>
<p>i = i || 100;<br />
j = j || 500;</p>
]]></content:encoded>
	</item>
</channel>
</rss>

