<?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: Constructor functions</title>
	<atom:link href="http://helephant.com/2008/09/constructor-functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://helephant.com/2008/09/constructor-functions/</link>
	<description></description>
	<lastBuildDate>Thu, 24 Jun 2010 11:27:14 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
	<item>
		<title>By: Javascript method context &#8211; Helephant.com</title>
		<link>http://helephant.com/2008/09/constructor-functions/#comment-3100</link>
		<dc:creator>Javascript method context &#8211; Helephant.com</dc:creator>
		<pubDate>Sun, 29 Nov 2009 18:27:44 +0000</pubDate>
		<guid isPermaLink="false">http://helephant.net/?p=408#comment-3100</guid>
		<description>[...] the previous examples you might have noticed that we use this to get a reference to the object that a function belongs to [...]</description>
		<content:encoded><![CDATA[<p>[...] the previous examples you might have noticed that we use this to get a reference to the object that a function belongs to [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Javascript prototype chaining - Helephant.com</title>
		<link>http://helephant.com/2008/09/constructor-functions/#comment-941</link>
		<dc:creator>Javascript prototype chaining - Helephant.com</dc:creator>
		<pubDate>Mon, 17 Aug 2009 09:29:02 +0000</pubDate>
		<guid isPermaLink="false">http://helephant.net/?p=408#comment-941</guid>
		<description>[...] Constructor functions have a property called prototype. Adding properties and methods to the prototype property will automatically add the method or property to all objects created by the constructor function. [...]</description>
		<content:encoded><![CDATA[<p>[...] Constructor functions have a property called prototype. Adding properties and methods to the prototype property will automatically add the method or property to all objects created by the constructor function. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ted devito</title>
		<link>http://helephant.com/2008/09/constructor-functions/#comment-940</link>
		<dc:creator>ted devito</dc:creator>
		<pubDate>Wed, 11 Mar 2009 17:31:31 +0000</pubDate>
		<guid isPermaLink="false">http://helephant.net/?p=408#comment-940</guid>
		<description>Yeah, it gets a little boggling. I found some crazy stuff on inheritance. Just an FYI, here&#039;s two pages that stood out:
http://ajaxpatterns.org/Javascript_Inheritance --- a wiki with a good summary
http://javascript.crockford.com/inheritance.html --- douglas crockford on inheritance (recommended by john resig)

After looking around at your tutorials and some of the links you recommended, I think what I was trying to do was something like this... The goal being to create multiple instances of an object that is populated with properties, but then each instance can have different local values to work with.

var existing = {prop1: &#039;&#039;, prop2: [], prop3: {}, prop4 : function (){} };
var constructor = function (a,b) {this.a = a; this.b = b;};
constructor.prototype = existing;
var instances = [], i = 0;

do {
instances.push( new constructor(i,somevar) ), i+=1;
} while (i &lt; 10)

for (var i in instances) alert( instances[i].prop1 );</description>
		<content:encoded><![CDATA[<p>Yeah, it gets a little boggling. I found some crazy stuff on inheritance. Just an FYI, here&#8217;s two pages that stood out:<br />
<a href="http://ajaxpatterns.org/Javascript_Inheritance" rel="nofollow">http://ajaxpatterns.org/Javascript_Inheritance</a> &#8212; a wiki with a good summary<br />
<a href="http://javascript.crockford.com/inheritance.html" rel="nofollow">http://javascript.crockford.com/inheritance.html</a> &#8212; douglas crockford on inheritance (recommended by john resig)</p>
<p>After looking around at your tutorials and some of the links you recommended, I think what I was trying to do was something like this&#8230; The goal being to create multiple instances of an object that is populated with properties, but then each instance can have different local values to work with.</p>
<p>var existing = {prop1: &#8221;, prop2: [], prop3: {}, prop4 : function (){} };<br />
var constructor = function (a,b) {this.a = a; this.b = b;};<br />
constructor.prototype = existing;<br />
var instances = [], i = 0;</p>
<p>do {<br />
instances.push( new constructor(i,somevar) ), i+=1;<br />
} while (i &lt; 10)</p>
<p>for (var i in instances) alert( instances[i].prop1 );</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Helen</title>
		<link>http://helephant.com/2008/09/constructor-functions/#comment-939</link>
		<dc:creator>Helen</dc:creator>
		<pubDate>Fri, 16 Jan 2009 22:16:54 +0000</pubDate>
		<guid isPermaLink="false">http://helephant.net/?p=408#comment-939</guid>
		<description>Glad you liked the article. :)

You can&#039;t just use the new operator with an object. It will only work with a function. This is because the constructor function will have logic in it to construct an object. It&#039;s not just cloning an existing one.

If you wanted to clone an existing object, you could iterate through all of its properties and copy them into a new object:
var person =
{
    prop1: &quot;one&quot;,
    prop2: &quot;two&quot;
}
var person2 = {};
for(var property in person)
{
    person2[property] = person[property]
}
alert(person2.prop1 + &quot; &quot; + person2.prop2);

There&#039;s an interesting article about doing this (make sure you check out the discussion) http://keithdevens.com/weblog/archive/2007/Jun/07/javascript.clone

Another way to do it is to use javascript&#039;s prototype chaining feature to make one object &quot;inherit&quot; from another:
function clone(existing)
{
    var constructor = function() {};
    constructor.prototype = existing;

    return new constructor();
}
var person3 = clone(person2);
alert(person3.prop1 + &quot; &quot; + person3.prop2);

It&#039;s a bit more complicated than regular inheritance, javascript isn&#039;t a class based language like java or C#, but it&#039;s a similar general idea. This is an idea that I want to talk about in a future article but there&#039;s plenty of good information about it on the web. Try googling for javascript prototype inheritance and javascript prototype chaining.

Thanks for the interesting question. :)</description>
		<content:encoded><![CDATA[<p>Glad you liked the article. :)</p>
<p>You can&#8217;t just use the new operator with an object. It will only work with a function. This is because the constructor function will have logic in it to construct an object. It&#8217;s not just cloning an existing one.</p>
<p>If you wanted to clone an existing object, you could iterate through all of its properties and copy them into a new object:<br />
var person =<br />
{<br />
    prop1: &#8220;one&#8221;,<br />
    prop2: &#8220;two&#8221;<br />
}<br />
var person2 = {};<br />
for(var property in person)<br />
{<br />
    person2[property] = person[property]<br />
}<br />
alert(person2.prop1 + &#8221; &#8221; + person2.prop2);</p>
<p>There&#8217;s an interesting article about doing this (make sure you check out the discussion) <a href="http://keithdevens.com/weblog/archive/2007/Jun/07/javascript.clone" rel="nofollow">http://keithdevens.com/weblog/archive/2007/Jun/07/javascript.clone</a></p>
<p>Another way to do it is to use javascript&#8217;s prototype chaining feature to make one object &#8220;inherit&#8221; from another:<br />
function clone(existing)<br />
{<br />
    var constructor = function() {};<br />
    constructor.prototype = existing;</p>
<p>    return new constructor();<br />
}<br />
var person3 = clone(person2);<br />
alert(person3.prop1 + &#8221; &#8221; + person3.prop2);</p>
<p>It&#8217;s a bit more complicated than regular inheritance, javascript isn&#8217;t a class based language like java or C#, but it&#8217;s a similar general idea. This is an idea that I want to talk about in a future article but there&#8217;s plenty of good information about it on the web. Try googling for javascript prototype inheritance and javascript prototype chaining.</p>
<p>Thanks for the interesting question. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ted devito</title>
		<link>http://helephant.com/2008/09/constructor-functions/#comment-938</link>
		<dc:creator>ted devito</dc:creator>
		<pubDate>Fri, 16 Jan 2009 03:48:36 +0000</pubDate>
		<guid isPermaLink="false">http://helephant.net/?p=408#comment-938</guid>
		<description>if you use object literals to define an object, can you instantiate new instances of that object? is there a key name as a constructor?

var myObj = {
dog:{},
cat:[],
goat: function(){},
construct: function(params) {}
}

var instances = [], i = 0;

do {
instances.push( new myObj(params) ), i+=1;
} while (i &lt; 10)

BY THE WAY, this is one of (well, tonight it&#039;s definitely the best!) javascript object tutorials i&#039;v e found. Your stuff is complete, clear and well written.</description>
		<content:encoded><![CDATA[<p>if you use object literals to define an object, can you instantiate new instances of that object? is there a key name as a constructor?</p>
<p>var myObj = {<br />
dog:{},<br />
cat:[],<br />
goat: function(){},<br />
construct: function(params) {}<br />
}</p>
<p>var instances = [], i = 0;</p>
<p>do {<br />
instances.push( new myObj(params) ), i+=1;<br />
} while (i &lt; 10)</p>
<p>BY THE WAY, this is one of (well, tonight it&#8217;s definitely the best!) javascript object tutorials i&#8217;v e found. Your stuff is complete, clear and well written.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Plodoc</title>
		<link>http://helephant.com/2008/09/constructor-functions/#comment-936</link>
		<dc:creator>Plodoc</dc:creator>
		<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://helephant.net/?p=408#comment-936</guid>
		<description>I think that every object will get a different copy of the sayHello function if you declare it in the constructor. If we want to mass produce objects, shouldn&#039;t we add the function to the Pet&#039;s prototype ?&lt;br /&gt;</description>
		<content:encoded><![CDATA[<p>I think that every object will get a different copy of the sayHello function if you declare it in the constructor. If we want to mass produce objects, shouldn&#8217;t we add the function to the Pet&#8217;s prototype ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Helen</title>
		<link>http://helephant.com/2008/09/constructor-functions/#comment-937</link>
		<dc:creator>Helen</dc:creator>
		<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://helephant.net/?p=408#comment-937</guid>
		<description>Yeah that&#039;s right. :) I&#039;m going to talk about prototypes in a couple of weeks.</description>
		<content:encoded><![CDATA[<p>Yeah that&#8217;s right. :) I&#8217;m going to talk about prototypes in a couple of weeks.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
