<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.1" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Default Arguments in JavaScript Functions</title>
	<link>http://parentnode.org/javascript/default-arguments-in-javascript-functions/</link>
	<description>The building blocks of a solid frontend.</description>
	<pubDate>Fri, 05 Dec 2008 01:34:36 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>

	<item>
		<title>By: fatbrain</title>
		<link>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-5916</link>
		<author>fatbrain</author>
		<pubDate>Thu, 25 Oct 2007 16:58:01 +0000</pubDate>
		<guid>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-5916</guid>
		<description>Thanks for input e-voc. What if we consider:

Function.prototype.defaults = function()
{
  var _f = this;
  var _a = Array(_f.length-arguments.length).concat(
    Array.prototype.slice.apply(arguments));
  return function()
  {
    return _f.apply(this, Array.prototype.slice.apply(arguments).concat(
      _a.slice(arguments.length, _a.length)));
  }
}

Or did I get it wrong? Anyhow, give it a testspin and get back to me.

Damn I'm hungry, need food for brain. (I only change the "this" argument in the apply on the inner function)

Cheers // fatbrain</description>
		<content:encoded><![CDATA[<p>Thanks for input e-voc. What if we consider:</p>
<p>Function.prototype.defaults = function()<br />
{<br />
  var _f = this;<br />
  var _a = Array(_f.length-arguments.length).concat(<br />
    Array.prototype.slice.apply(arguments));<br />
  return function()<br />
  {<br />
    return _f.apply(this, Array.prototype.slice.apply(arguments).concat(<br />
      _a.slice(arguments.length, _a.length)));<br />
  }<br />
}</p>
<p>Or did I get it wrong? Anyhow, give it a testspin and get back to me.</p>
<p>Damn I&#8217;m hungry, need food for brain. (I only change the &#8220;this&#8221; argument in the apply on the inner function)</p>
<p>Cheers // fatbrain</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: e-voc</title>
		<link>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-4948</link>
		<author>e-voc</author>
		<pubDate>Mon, 17 Sep 2007 15:34:02 +0000</pubDate>
		<guid>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-4948</guid>
		<description>For normal functions, one would pass null or window as execution context

var bar = function (who) { return who; }.defaults(null, 'me');
bar();

Regards
e-voc</description>
		<content:encoded><![CDATA[<p>For normal functions, one would pass null or window as execution context</p>
<p>var bar = function (who) { return who; }.defaults(null, &#8216;me&#8217;);<br />
bar();</p>
<p>Regards<br />
e-voc</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: e-voc</title>
		<link>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-4947</link>
		<author>e-voc</author>
		<pubDate>Mon, 17 Sep 2007 15:30:43 +0000</pubDate>
		<guid>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-4947</guid>
		<description>sorry, the last on was buggy.
here's the working one:

Function.prototype.defaults = function(o) {
  var _f = this;
  var args = Array.prototype.slice.call(arguments, 1);
  return function() {
    var a = Array.prototype.slice.call(arguments);
    return _f.apply(o, a.concat(args.slice(a.length)));
  }
}

function Foo() { this.bla = 1; }
Foo.prototype.foo = function(a, b) {
  return this.bla + ':' + a + '/' + b;
};

var f = new Foo;
f.foo = f.foo.defaults(f, 42, 'default_b');

f.foo(10, 20); // returns "1:10/20"
f.foo(10); // returns "1:10/default_b"
f.foo(); // returns "1:42/default_b"</description>
		<content:encoded><![CDATA[<p>sorry, the last on was buggy.<br />
here&#8217;s the working one:</p>
<p>Function.prototype.defaults = function(o) {<br />
  var _f = this;<br />
  var args = Array.prototype.slice.call(arguments, 1);<br />
  return function() {<br />
    var a = Array.prototype.slice.call(arguments);<br />
    return _f.apply(o, a.concat(args.slice(a.length)));<br />
  }<br />
}</p>
<p>function Foo() { this.bla = 1; }<br />
Foo.prototype.foo = function(a, b) {<br />
  return this.bla + &#8216;:&#8217; + a + &#8216;/&#8217; + b;<br />
};</p>
<p>var f = new Foo;<br />
f.foo = f.foo.defaults(f, 42, &#8216;default_b&#8217;);</p>
<p>f.foo(10, 20); // returns &#8220;1:10/20&#8243;<br />
f.foo(10); // returns &#8220;1:10/default_b&#8221;<br />
f.foo(); // returns &#8220;1:42/default_b&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: e-voc</title>
		<link>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-4946</link>
		<author>e-voc</author>
		<pubDate>Mon, 17 Sep 2007 15:07:20 +0000</pubDate>
		<guid>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-4946</guid>
		<description>Your way won't work with objects.

This way will: http://phpfi.com/263389

Regards
e-voc</description>
		<content:encoded><![CDATA[<p>Your way won&#8217;t work with objects.</p>
<p>This way will: <a href="http://phpfi.com/263389" rel="nofollow">http://phpfi.com/263389</a></p>
<p>Regards<br />
e-voc</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: colossalus</title>
		<link>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-3427</link>
		<author>colossalus</author>
		<pubDate>Fri, 13 Jul 2007 07:04:51 +0000</pubDate>
		<guid>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-3427</guid>
		<description>I have a hybrid idea that uses your method with associative arrays:
&lt;a&gt;http://www.adventsun.com/code/JS_Function_Defaults.htm&lt;/a&gt;

Thanks for pointing me in the right direction! :)</description>
		<content:encoded><![CDATA[<p>I have a hybrid idea that uses your method with associative arrays:<br />
<a>http://www.adventsun.com/code/JS_Function_Defaults.htm</a></p>
<p>Thanks for pointing me in the right direction! :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fatbrain</title>
		<link>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-3343</link>
		<author>fatbrain</author>
		<pubDate>Fri, 06 Jul 2007 10:16:28 +0000</pubDate>
		<guid>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-3343</guid>
		<description>Thanks for all your lovely comments, very much appreciated :-)</description>
		<content:encoded><![CDATA[<p>Thanks for all your lovely comments, very much appreciated :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stefan</title>
		<link>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-3231</link>
		<author>Stefan</author>
		<pubDate>Mon, 02 Jul 2007 11:07:33 +0000</pubDate>
		<guid>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-3231</guid>
		<description>Nice idea and elegant implementation, although I think I am not going to use it because:
1. it is less readable than the "if typeof" version or the shorter "= .. &#124;&#124; ..", because the defaults are seperated (lines of code wise) far from the argument list.
2. if you add another argument to the function you have to update the default list, which might lead to hard to find processing errors. Maybe one could work around by requiring that the default call always get's the same number of arguments as the function itself expects, causing an error if it doesn't.</description>
		<content:encoded><![CDATA[<p>Nice idea and elegant implementation, although I think I am not going to use it because:<br />
1. it is less readable than the &#8220;if typeof&#8221; version or the shorter &#8220;= .. || ..&#8221;, because the defaults are seperated (lines of code wise) far from the argument list.<br />
2. if you add another argument to the function you have to update the default list, which might lead to hard to find processing errors. Maybe one could work around by requiring that the default call always get&#8217;s the same number of arguments as the function itself expects, causing an error if it doesn&#8217;t.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-3147</link>
		<author>Scott</author>
		<pubDate>Fri, 29 Jun 2007 17:56:20 +0000</pubDate>
		<guid>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-3147</guid>
		<description>I've read hundreds of articles containing js snippets for accomplishing various things, most of which are either no-brainers or completely irrational.

This one is not. Well done.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve read hundreds of articles containing js snippets for accomplishing various things, most of which are either no-brainers or completely irrational.</p>
<p>This one is not. Well done.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Binny V A</title>
		<link>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-3136</link>
		<author>Binny V A</author>
		<pubDate>Fri, 29 Jun 2007 12:19:12 +0000</pubDate>
		<guid>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-3136</guid>
		<description>In Javascript, I use 3 different methods to get this effect...
&lt;a href="http://www.openjs.com/articles/optional_function_arguments.php" rel="nofollow"&gt;Optional Function Arguments in JavaScript&lt;/a&gt;

Your method is the fourth - Thanks!</description>
		<content:encoded><![CDATA[<p>In Javascript, I use 3 different methods to get this effect&#8230;<br />
<a href="http://www.openjs.com/articles/optional_function_arguments.php" rel="nofollow">Optional Function Arguments in JavaScript</a></p>
<p>Your method is the fourth - Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ariel</title>
		<link>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-2177</link>
		<author>Ariel</author>
		<pubDate>Mon, 07 May 2007 19:37:28 +0000</pubDate>
		<guid>http://parentnode.org/javascript/default-arguments-in-javascript-functions/#comment-2177</guid>
		<description>I added a "minor than" symbol in the previous post and the post is not showing properly, I'll repost it without the symbol.

fatbrain, I loved your code, I was trying to code something like this but I couldn't, so I searched and found yours, I modified it a bit so that it won't throw an error if the defaults array is larger than the arguments one. Also will update parameters that are submitted as null or undefined.
All this changes are not much relevant.. the only problem I found is that you were passing _f as scope of the Function.apply.. instead I think it should be receiving "this" to behave as expected. here's my version.

Function.prototype.defaults = function(){
  var f = this;
  var def = Array.prototype.slice.apply(arguments);
  function update(a){
  	var na = Array.prototype.slice.apply(a);
  	for(var i=0;i</description>
		<content:encoded><![CDATA[<p>I added a &#8220;minor than&#8221; symbol in the previous post and the post is not showing properly, I&#8217;ll repost it without the symbol.</p>
<p>fatbrain, I loved your code, I was trying to code something like this but I couldn&#8217;t, so I searched and found yours, I modified it a bit so that it won&#8217;t throw an error if the defaults array is larger than the arguments one. Also will update parameters that are submitted as null or undefined.<br />
All this changes are not much relevant.. the only problem I found is that you were passing _f as scope of the Function.apply.. instead I think it should be receiving &#8220;this&#8221; to behave as expected. here&#8217;s my version.</p>
<p>Function.prototype.defaults = function(){<br />
  var f = this;<br />
  var def = Array.prototype.slice.apply(arguments);<br />
  function update(a){<br />
  	var na = Array.prototype.slice.apply(a);<br />
  	for(var i=0;i</p>
]]></content:encoded>
	</item>
</channel>
</rss>
