tag:blogger.com,1999:blog-6160066215788757628.post1016386347107113644..comments2017-11-27T02:18:23.287-08:00Comments on Higher-Order: JavaScript parasitic inheritance, power constructors and instanceof.krukowhttp://www.blogger.com/profile/02045796732071392830noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-6160066215788757628.post-29517874515391803432013-11-22T07:40:47.894-08:002013-11-22T07:40:47.894-08:00The link given is invalid, it should be http://blo...The link given is invalid, it should be http://blog.higher-order.net/2008/02/21/javascript-parasitic-inheritance-power-constructors-and-instanceof.htmlAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-6160066215788757628.post-64122541992969195812009-10-28T13:26:11.856-07:002009-10-28T13:26:11.856-07:00This blog has moved to:
http://blog.higher-order....This blog has moved to:<br /><br />http://blog.higher-order.net<br /><br />Please post commments and questions there.<br /><br />This particular entry is located at:<br /><br />http://blog.higher-order.net/2008/02/21/javascript-parasitic-inheritance-power-constructors-and-instanceof/krukowhttps://www.blogger.com/profile/02045796732071392830noreply@blogger.comtag:blogger.com,1999:blog-6160066215788757628.post-64937370354655254102009-10-28T13:13:04.855-07:002009-10-28T13:13:04.855-07:00--Anonymous--
The classical patter you show is gre...--Anonymous--<br />The classical patter you show is great but you should realize that as soon as you define a reference type in the chain all instance will see changes so you&#39;ll essentially have statics. So if your OriginalPerson object had var arr = [1,2,3], you&#39;d inherit that for all sub-instances and they&#39;d &quot;share state&quot; so you&#39;d have a static. You should use &quot;constructor stealing&quot; to prevent that - basically, the sub types call the super&#39;s constructor so that each instance has it&#39;s own state. <br /><br />Wait, but that&#39;ll mean two calls to the parent constructor, one for the constructor stealing and one when you say child.prototype = new SuperType() So the next thing to do is create a helper function to essentially instantiatePrototypes(childConstructor, parentConstructor) that points the child&#39;s proto to the parent&#39;s proto, and then you only call the parent constructor once and don&#39;t get all the parent&#39;s instance variable &quot;junk&quot; on the sub type. <br /><br />Impressed? Don&#39;t be, this is completely derivative as I&#39;m paraphrasing Nicholas Zakas&#39;s book (end of Chapter 6). Zakas Professional Javascript... Chapter 6 and Crockford Good Parts Chapter 3 &amp; 5 are wonderful to read together for grokking creation and inheritance in JS! I&#39;m actually doing a TDD analysis of them in my &quot;pamphlet&quot; on testing javascript: <br /><a href="http://github.com/roblevintennis/Testing-and-Debugging-JavaScript" rel="nofollow">Testing Javascript</a>Rob Levinhttps://www.blogger.com/profile/14052607630555371885noreply@blogger.comtag:blogger.com,1999:blog-6160066215788757628.post-86377384783624417712008-11-11T21:10:00.000-08:002008-11-11T21:10:00.000-08:00This blog has moved to:http://blog.higher-order.ne...This blog has moved to:<BR/><BR/>http://blog.higher-order.net<BR/><BR/>Please post commments and questions there.<BR/><BR/>This particular entry is located at:<BR/><BR/>http://blog.higher-order.net/2008/02/21/javascript-parasitic-inheritance-power-constructors-and-instanceof/krukowhttps://www.blogger.com/profile/02045796732071392830noreply@blogger.comtag:blogger.com,1999:blog-6160066215788757628.post-37181542918367720672008-11-11T18:36:00.000-08:002008-11-11T18:36:00.000-08:00I'm not sure what the benefit of doing all that ov...I'm not sure what the benefit of doing all that over this is:<BR/><BR/>function OriginalPerson() { <BR/> this.sayHello = function() { <BR/> alert("Hello, my name is " + this.getName()); <BR/> };<BR/><BR/> this.getName = function() {<BR/> return 'Adam';<BR/> }<BR/>}<BR/><BR/>function Person(name) { <BR/> this.name = name; <BR/> this.getName = function() {return this.name;} <BR/>} <BR/>Person.prototype = new OriginalPerson; <BR/><BR/>function Guru(name, topic) { <BR/> this.name = name;<BR/> this.topic = topic;<BR/> this.getTopic = function() { return this.topic; }; <BR/>}<BR/>Guru.prototype = new Person;<BR/><BR/>var mary = new Person("mary");<BR/>var someone = new Guru("someone", "something");<BR/>alert(mary instanceof Person);<BR/>alert(someone instanceof Person);<BR/>alert(someone instanceof Guru);<BR/><BR/>All of the instanceof checks return true. <BR/><BR/>I also think your object function may have a bug? Shouldn't F.prototype = <B>new</B> o; instead of just o?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6160066215788757628.post-55584876539480552922008-09-17T21:17:00.000-07:002008-09-17T21:17:00.000-07:00This blog has moved to:http://blog.higher-order.ne...This blog has moved to:<BR/><BR/>http://blog.higher-order.net<BR/><BR/>Please post commments and questions there.<BR/><BR/>This particular entry is located at:<BR/><BR/><A HREF="http://blog.higher-order.net/2008/02/21/javascript-parasitic-inheritance-power-constructors-and-instanceof/" REL="nofollow">http://blog.higher-order.net/2008/02/21/javascript-parasitic-inheritance-power-constructors-and-instanceof/</A>Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6160066215788757628.post-15516746872781053202008-09-17T12:39:00.000-07:002008-09-17T12:39:00.000-07:00I thought extending Object.prototype was a bad ide...I thought extending Object.prototype was a bad idea. Is that really needed?Mehttps://www.blogger.com/profile/05527008723676623262noreply@blogger.comtag:blogger.com,1999:blog-6160066215788757628.post-88551445613481694802008-07-01T17:57:00.000-07:002008-07-01T17:57:00.000-07:00p = proto;//exercise: why?I believe this is due to...p = proto;//exercise: why?<BR/><BR/>I believe this is due to how Javascript's closures work.AaronNGrayhttps://www.blogger.com/profile/17461772153398132474noreply@blogger.com