<?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"
	>
<channel>
	<title>Comments on: Sending objects as parameters - good or bad?</title>
	<atom:link href="http://www.wait-till-i.com/2007/11/07/sending-object-as-parameters-good-or-bad/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wait-till-i.com/2007/11/07/sending-object-as-parameters-good-or-bad/</link>
	<description>Chris Heilmann - Accessibility, Web Development and Pragmatism - can talk, will travel</description>
	<pubDate>Fri, 05 Dec 2008 11:01:20 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: sam</title>
		<link>http://www.wait-till-i.com/2007/11/07/sending-object-as-parameters-good-or-bad/#comment-8605</link>
		<dc:creator>sam</dc:creator>
		<pubDate>Fri, 14 Nov 2008 18:52:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.wait-till-i.com/2007/11/07/sending-object-as-parameters-good-or-bad/#comment-8605</guid>
		<description>Just glancing through the comments, I wanted to note a couple of things.

First, "matt snider" suggests this for default values:
params.item1 = params.item1 &#124;&#124; defaultValue1;

However, this is a bug.  In cases where you set "item1" to 0, false, null, empty string (anything that == false), it will be overwritten with defaultValue1.  The proper method would be:

params.item1 = params.hasOwnProperty("item1") ? params.item1 : 0;


Aside from that, I too am conditioned with PHP... however I *love* the way you can pass objects in javascript, as the author suggests.  PHP, on the contrary, has "default parameters" but the implementation leaves more to be desired.  I won't get into it too much, but heres an example:

//PHP code
function foo($required, $optional1="some long string", $optional2=null)

//you cannot "skip" an optional parameter:
foo($some_required_value, ,$not_null);  //I wish you could do this... instead you have to write optiona1 out
foo($some_required_value, "some long string", $not_null); //gets worse with more parameters :(


-sam</description>
		<content:encoded><![CDATA[<p>Just glancing through the comments, I wanted to note a couple of things.</p>
<p>First, &#8220;matt snider&#8221; suggests this for default values:<br />
params.item1 = params.item1 || defaultValue1;</p>
<p>However, this is a bug.  In cases where you set &#8220;item1&#8243; to 0, false, null, empty string (anything that == false), it will be overwritten with defaultValue1.  The proper method would be:</p>
<p>params.item1 = params.hasOwnProperty(&#8221;item1&#8243;) ? params.item1 : 0;</p>
<p>Aside from that, I too am conditioned with <span class="caps">PHP&#8230; </span>however I <strong>love</strong> the way you can pass objects in javascript, as the author suggests.  <span class="caps">PHP, </span>on the contrary, has &#8220;default parameters&#8221; but the implementation leaves more to be desired.  I won&#8217;t get into it too much, but heres an example:</p>
<p>//PHP code<br />
function foo($required, $optional1=&#8221;some long string&#8221;, $optional2=null)</p>
<p>//you cannot &#8220;skip&#8221; an optional parameter:<br />
foo($some_required_value, ,$not_null);  //I wish you could do this&#8230; instead you have to write optiona1 out<br />
foo($some_required_value, &#8220;some long string&#8221;, $not_null); //gets worse with more parameters :(</p>
<p>-sam</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Emmett</title>
		<link>http://www.wait-till-i.com/2007/11/07/sending-object-as-parameters-good-or-bad/#comment-6394</link>
		<dc:creator>Emmett</dc:creator>
		<pubDate>Tue, 13 Nov 2007 22:08:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.wait-till-i.com/2007/11/07/sending-object-as-parameters-good-or-bad/#comment-6394</guid>
		<description>Absolutely agreed, with the caveat (mentioned by others) that any *required* attribute should be an actual parameter.  The "cfg" object (as I almost always name it) is for optional arguments.</description>
		<content:encoded><![CDATA[<p>Absolutely agreed, with the caveat (mentioned by others) that any <strong>required</strong> attribute should be an actual parameter.  The &#8220;cfg&#8221; object (as I almost always name it) is for optional arguments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stuart Langridge</title>
		<link>http://www.wait-till-i.com/2007/11/07/sending-object-as-parameters-good-or-bad/#comment-6335</link>
		<dc:creator>Stuart Langridge</dc:creator>
		<pubDate>Fri, 09 Nov 2007 11:31:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.wait-till-i.com/2007/11/07/sending-object-as-parameters-good-or-bad/#comment-6335</guid>
		<description>Let us be honest, now: this is basically a bodge to get around JS's lack of named parameters and the awkwardness of dealing with a variable length parameter list. However, since we *need* a bodge in this area, this is the one I normally use...</description>
		<content:encoded><![CDATA[<p>Let us be honest, now: this is basically a bodge to get around <span class="caps">JS&#8217;</span>s lack of named parameters and the awkwardness of dealing with a variable length parameter list. However, since we <strong>need</strong> a bodge in this area, this is the one I normally use&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lindsay</title>
		<link>http://www.wait-till-i.com/2007/11/07/sending-object-as-parameters-good-or-bad/#comment-6309</link>
		<dc:creator>Lindsay</dc:creator>
		<pubDate>Wed, 07 Nov 2007 18:58:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.wait-till-i.com/2007/11/07/sending-object-as-parameters-good-or-bad/#comment-6309</guid>
		<description>Definitely agree.  Theres a way to support both kinds as well, or to be backward compatible, for example to replace a multi-arg function with one that takes a single object literal:

function foo() {
   if (arguments.length &#62; 1) {
       doFoo.apply(this, arguments);
   } else {
       var args = arguments[0]
       doFoo(args.a1, args.a2, args.a3, args.a4, args.a5);
   }
}

function doFoo(a1, a2, a3, a4, a5) {

}</description>
		<content:encoded><![CDATA[<p>Definitely agree.  Theres a way to support both kinds as well, or to be backward compatible, for example to replace a multi-arg function with one that takes a single object literal:</p>
<p>function foo() {<br />
   if (arguments.length &gt; 1) {<br />
       doFoo.apply(this, arguments);<br />
   } else {<br />
       var args = arguments<sup class="footnote"><a href="#fn0">0</a></sup><br />
       doFoo(args.a1, args.a2, args.a3, args.a4, args.a5);<br />
   }<br />
}</p>
<p>function doFoo(a1, a2, a3, a4, a5) {</p>
<p>}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew</title>
		<link>http://www.wait-till-i.com/2007/11/07/sending-object-as-parameters-good-or-bad/#comment-6305</link>
		<dc:creator>Andrew</dc:creator>
		<pubDate>Wed, 07 Nov 2007 13:09:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.wait-till-i.com/2007/11/07/sending-object-as-parameters-good-or-bad/#comment-6305</guid>
		<description>Another option is to use query strings which are parsed into variables. I'm not sure it really has any real advantages but they can be easier to work with in some situations.</description>
		<content:encoded><![CDATA[<p>Another option is to use query strings which are parsed into variables. I&#8217;m not sure it really has any real advantages but they can be easier to work with in some situations.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
