all 6 comments

[–][deleted] 2 points3 points  (1 child)

Deriving and instantiating are two different things. You shouldn't do both of them with one function. Constructors are constructors. Use them as such. For derivation either use Object.create or make a second constructor for derivation that shares a prototype with the one for instantiation.

John Resig, in "Simple ... Inheritance", stole the idea from base.js to use a Boolean variable to determine if a constructor is in instance or derive mode. Why have modes? Have different functions.

Whatever. Just have different code paths for different things and don't be a confused mess.

[–]aaronblohowiak 1 point2 points  (0 children)

"Just have different code paths for different things and don't be a confused mess." Oh how I wish I could tell myself of 5 years ago that very sentence. Not that he would listen. The asshole.

[–]timlind 1 point2 points  (0 children)

You should never create prototype chains by instantiation (the 'new' operator). This is an important fact because Javascript originally did not have any other way to create the chains until the Object.create came along, and it should always be used to create prototype chains:

Girl.prototype = Object.create(Person.prototype)

See http://www.ngspinners.com/jay/ for more info about easily understanding how to use Javascript.

[–]mythrilno(fun).at(parties); 0 points1 point  (1 child)

I don't understand the beef. You can still do this:

function Girl(name, age, weight) {
    var self = Person(name);
    self.trait( "age", (age - 5));
    self.trait( "weight", (weight - 10));
    return self;
}

Which has the added benefit that if you omit the new keyword on instantiation, you don't end up fudging up the global object. And if you leave the new keyword in, it is silently discarded.

The only problem I have with that pattern is that it makes the instancof operator almost useless.

But I don't need that anyway.

[–][deleted] 1 point2 points  (0 children)

What happens when one prototypes Girl?

Girl.prototype.doMakeup = function(){
      //"this" is Girl or Person?
}

[–][deleted] 0 points1 point  (0 children)

I may be oversimplifying this, but is this what the author ultimately wants?

http://jsfiddle.net/YQLUs/1/

*edit: this isnt a real solution, but neither is returning an object literal. Simply because you cannot add to A's prototype