<?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: Creating JavaScript Classes, Part 5: The Class Function (with screencast)</title>
	<atom:link href="http://livingmachines.net/2009/03/creating-javascript-classes-part-5-the-class-function/feed/" rel="self" type="application/rss+xml" />
	<link>http://livingmachines.net/2009/03/creating-javascript-classes-part-5-the-class-function/</link>
	<description>Test-driven development of a JavaScript MVC library, from the ground up...</description>
	<lastBuildDate>Sat, 07 Jan 2012 08:27:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Jason S. Kerchner</title>
		<link>http://livingmachines.net/2009/03/creating-javascript-classes-part-5-the-class-function/comment-page-1/#comment-649</link>
		<dc:creator>Jason S. Kerchner</dc:creator>
		<pubDate>Fri, 12 Nov 2010 03:58:07 +0000</pubDate>
		<guid isPermaLink="false">http://livingmachines.net/?p=235#comment-649</guid>
		<description>Go for it, Ian. All I ask is that you place a link from your site to the original source code here. That way the conversation doesn&#039;t get broken.</description>
		<content:encoded><![CDATA[<p>Go for it, Ian. All I ask is that you place a link from your site to the original source code here. That way the conversation doesn&#8217;t get broken.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian</title>
		<link>http://livingmachines.net/2009/03/creating-javascript-classes-part-5-the-class-function/comment-page-1/#comment-648</link>
		<dc:creator>Ian</dc:creator>
		<pubDate>Wed, 10 Nov 2010 20:12:48 +0000</pubDate>
		<guid isPermaLink="false">http://livingmachines.net/?p=235#comment-648</guid>
		<description>Hi Jason,

It has always been my feeling that programmers should be forced to use slow machines as this (nearly) always forces us to write better, more efficient code.  Hiding where the browser is doing extra, unnecessary work doesn&#039;t help in the long run.

Yes, I realise my example doesn&#039;t work, otherwise I&#039;d have posted a working, loop-free version :-)
I did try quite a few things (even bitwise XOR) to see if you can trick the JS engine into performing the merge without the loop, to no avail.

I have created a function that performs some of your requirements, the removal of the two lines that assign the prototype and the base, whilst it also removes the need for the extra global inheriting object and the check for if (arguments[0] === inheriting) return;.

Both of which also felt wrong to me.

I&#039;m a professional programmer, but not in JavaScript so would appreciate any insight in to what I&#039;ve probably done wrong in my implementation of a createClass function.  As such, rather than try to paste code here (which never works well in comments) if I may have your permission to republish your examples modified to use my function to my website and then I&#039;ll post the link back here.

Ian.</description>
		<content:encoded><![CDATA[<p>Hi Jason,</p>
<p>It has always been my feeling that programmers should be forced to use slow machines as this (nearly) always forces us to write better, more efficient code.  Hiding where the browser is doing extra, unnecessary work doesn&#8217;t help in the long run.</p>
<p>Yes, I realise my example doesn&#8217;t work, otherwise I&#8217;d have posted a working, loop-free version <img src='http://livingmachines.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
I did try quite a few things (even bitwise XOR) to see if you can trick the JS engine into performing the merge without the loop, to no avail.</p>
<p>I have created a function that performs some of your requirements, the removal of the two lines that assign the prototype and the base, whilst it also removes the need for the extra global inheriting object and the check for if (arguments[0] === inheriting) return;.</p>
<p>Both of which also felt wrong to me.</p>
<p>I&#8217;m a professional programmer, but not in JavaScript so would appreciate any insight in to what I&#8217;ve probably done wrong in my implementation of a createClass function.  As such, rather than try to paste code here (which never works well in comments) if I may have your permission to republish your examples modified to use my function to my website and then I&#8217;ll post the link back here.</p>
<p>Ian.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason S. Kerchner</title>
		<link>http://livingmachines.net/2009/03/creating-javascript-classes-part-5-the-class-function/comment-page-1/#comment-641</link>
		<dc:creator>Jason S. Kerchner</dc:creator>
		<pubDate>Fri, 05 Nov 2010 23:45:52 +0000</pubDate>
		<guid isPermaLink="false">http://livingmachines.net/?p=235#comment-641</guid>
		<description>Hi Ian,

In general, yes you should avoid looping whenever possible as it does tend to slow down code. And JavaScript, being an interpreted language, is already fairly slow (though newer JavaScript engines are addressing that). I don&#039;t recall in which screencast I talked about adding functions by looping, but I think it was in the context of copying &lt;strong&gt;all&lt;/strong&gt; the methods of a class. In this case, we are only copying the methods that are being added to the descendant class. I&#039;d love to come up with a way to do it without looping.

In your code example, the OR operator, in the way you&#039;ve used it, will assign whichever of the two values is not null or undefined. That means that it won&#039;t override a function that already exists in cstr.prototype. In other words, if cstr.prototype[p] is not null or undefined then it will be assigned to cstr.prototype[p]. It will only assign init[p] if it does not already exist in cstr.prototype.

Oh, and by the way, feel free to (respectfully) criticize my work. That&#039;s how we all learn.</description>
		<content:encoded><![CDATA[<p>Hi Ian,</p>
<p>In general, yes you should avoid looping whenever possible as it does tend to slow down code. And JavaScript, being an interpreted language, is already fairly slow (though newer JavaScript engines are addressing that). I don&#8217;t recall in which screencast I talked about adding functions by looping, but I think it was in the context of copying <strong>all</strong> the methods of a class. In this case, we are only copying the methods that are being added to the descendant class. I&#8217;d love to come up with a way to do it without looping.</p>
<p>In your code example, the OR operator, in the way you&#8217;ve used it, will assign whichever of the two values is not null or undefined. That means that it won&#8217;t override a function that already exists in cstr.prototype. In other words, if cstr.prototype[p] is not null or undefined then it will be assigned to cstr.prototype[p]. It will only assign init[p] if it does not already exist in cstr.prototype.</p>
<p>Oh, and by the way, feel free to (respectfully) criticize my work. That&#8217;s how we all learn.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian</title>
		<link>http://livingmachines.net/2009/03/creating-javascript-classes-part-5-the-class-function/comment-page-1/#comment-640</link>
		<dc:creator>Ian</dc:creator>
		<pubDate>Fri, 05 Nov 2010 19:26:05 +0000</pubDate>
		<guid isPermaLink="false">http://livingmachines.net/?p=235#comment-640</guid>
		<description>(I&#039;d just like to prefix my comment with this disclaimer: I&#039;m not critising your work in any way, these are a very informative and helpful series of tutorials, but I feel slightly disapointed at what must be a limitation of javascript, it&#039;s just that the loop _Feels Wrong_)

In a previous screencase you mention that we shouldn&#039;t loop to copy the methods due to the time to copy the methods of large classes and the fact that the addition of new methods to the prototype are not then available in the child.

In this example you loop to copy the methods onto the new constructor.  Isn&#039;t there any sort of &quot;OR&quot; in a similar vain the the bitwise operator that would allow both sets of methods to be created without the loop, i.e.,
Isn&#039;t there a way to replace:
for (var p in init)
    cstr.prototype[p] = init[p];

with something similar to:
cstr.prototype[p] = cstr.prototype[p] &#124; init[p];</description>
		<content:encoded><![CDATA[<p>(I&#8217;d just like to prefix my comment with this disclaimer: I&#8217;m not critising your work in any way, these are a very informative and helpful series of tutorials, but I feel slightly disapointed at what must be a limitation of javascript, it&#8217;s just that the loop _Feels Wrong_)</p>
<p>In a previous screencase you mention that we shouldn&#8217;t loop to copy the methods due to the time to copy the methods of large classes and the fact that the addition of new methods to the prototype are not then available in the child.</p>
<p>In this example you loop to copy the methods onto the new constructor.  Isn&#8217;t there any sort of &#8220;OR&#8221; in a similar vain the the bitwise operator that would allow both sets of methods to be created without the loop, i.e.,<br />
Isn&#8217;t there a way to replace:<br />
for (var p in init)<br />
    cstr.prototype[p] = init[p];</p>
<p>with something similar to:<br />
cstr.prototype[p] = cstr.prototype[p] | init[p];</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Olga</title>
		<link>http://livingmachines.net/2009/03/creating-javascript-classes-part-5-the-class-function/comment-page-1/#comment-620</link>
		<dc:creator>Olga</dc:creator>
		<pubDate>Wed, 06 Oct 2010 18:20:19 +0000</pubDate>
		<guid isPermaLink="false">http://livingmachines.net/?p=235#comment-620</guid>
		<description>I&#039;m really happy that I found your series. Thanks a lot.</description>
		<content:encoded><![CDATA[<p>I&#8217;m really happy that I found your series. Thanks a lot.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason S. Kerchner</title>
		<link>http://livingmachines.net/2009/03/creating-javascript-classes-part-5-the-class-function/comment-page-1/#comment-558</link>
		<dc:creator>Jason S. Kerchner</dc:creator>
		<pubDate>Tue, 15 Dec 2009 16:50:23 +0000</pubDate>
		<guid isPermaLink="false">http://livingmachines.net/?p=235#comment-558</guid>
		<description>Hey Kevin, glad you found the information helpful. JavaScript can be a tricky beast, but keep at it and you&#039;ll get it.</description>
		<content:encoded><![CDATA[<p>Hey Kevin, glad you found the information helpful. JavaScript can be a tricky beast, but keep at it and you&#8217;ll get it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin</title>
		<link>http://livingmachines.net/2009/03/creating-javascript-classes-part-5-the-class-function/comment-page-1/#comment-557</link>
		<dc:creator>Kevin</dc:creator>
		<pubDate>Thu, 10 Dec 2009 15:42:13 +0000</pubDate>
		<guid isPermaLink="false">http://livingmachines.net/?p=235#comment-557</guid>
		<description>Wow, good stuff. The only thing I don&#039;t really understand about Javascript is this stuff.
Thanks</description>
		<content:encoded><![CDATA[<p>Wow, good stuff. The only thing I don&#8217;t really understand about Javascript is this stuff.<br />
Thanks</p>
]]></content:encoded>
	</item>
</channel>
</rss>

