2

In his sitepoint article about javascript inheritance, Harry Fuecks explains a way of implementing inheritance as follows:

    function copyPrototype(descendant, parent) {
    var sConstructor = parent.toString();
    var aMatch = sConstructor.match( /\s*function (.*)\(/ );
    if ( aMatch != null ) { descendant.prototype[aMatch[1]] = parent; }
    for (var m in parent.prototype) {
        descendant.prototype[m] = parent.prototype[m];
    }
};

While I understand his code, one question comes to mind - why not remove the for loop and simply do this:

 function copyPrototype(descendant, parent) {
    var sConstructor = parent.toString();
    var aMatch = sConstructor.match( /\s*function (.*)\(/ );
    if ( aMatch != null ) { descendant.prototype[aMatch[1]] = parent; }
    descendant.prototype = parent.prototype;
};

Thanks.

demongolem
  • 9,474
  • 36
  • 90
  • 105
jd.
  • 4,057
  • 7
  • 37
  • 45
  • 3
    That's a terrible way to implement inheritance - function decompilation, manual copying of properties (without even taking care of DontEnum bug) instead of more efficient prototypical approach, etc. – kangax Oct 02 '09 at 02:46
  • Feel free to post your way of doing it – jd. Oct 02 '09 at 04:03
  • 1
    I would suggest to read Crockford's article (http://www.crockford.com/javascript/inheritance.html) for a simple beginner-oriented overview of inheritance in JS. For a deeper understanding, look into Michaux's (http://peter.michaux.ca/articles/class-based-inheritance-in-javascript) or Giammarchi (http://www.3site.eu/doc/) writings. – kangax Oct 02 '09 at 05:09
  • Check this out: http://stackoverflow.com/questions/7486825/javascript-inheritance/12816953#12816953 – Lorenzo Polidori Oct 10 '12 at 10:36

1 Answers1

3

Assigning the prototype of one function to another would only assign a reference to the original prototype; both would be sharing the same prototype object. Iterating through the prototype creates a shallow copy of all its members.

Alex Barrett
  • 16,175
  • 3
  • 52
  • 51
  • 1
    Wouldn't that be what is wanted when inheriting from another object? Shouldn't any changes to the parent be reflected in the descendants? – jd. Oct 02 '09 at 01:16
  • Under some circumstances, yes. But you certainly don't want changes to the descendant reflected in the parent. – Alex Barrett Oct 02 '09 at 01:18
  • Instead of assigning the prototype of one function to another I assign the prototype of my "subclass" function to a new MyParentClass() as outlined on page 169 of the Javascript Rhino book. Is that wrong/bad? PositionedRectangle.prototype = new Rectangle(); – Matthew Lock Oct 02 '09 at 02:19
  • **@Matthew Lock:** This is clever technique making use of the prototype model. When using the `new` operator on a function, an `Object` is created and the members of the constructor's prototype are copied to that object. By using this new (entirely separate) object as our descendent's prototype, we have effectively created a copy of the original prototype. – Alex Barrett Oct 02 '09 at 02:35
  • Does that mean that the for loop in the question above can just be replaced by descendant.prototype = new parent(); ? – jd. Oct 02 '09 at 03:58
  • **@Soca:** The downside to using the `new Parent()` approach is that the parent's constructor _may_ have side-effects. Initializing a concrete `Rectangle` is one thing - you know exactly what's happening - but when you are creating a generic inheritance model as in your original question, you cannot be sure that it's completely safe to do so. – Alex Barrett Oct 02 '09 at 14:39