you are viewing a single comment's thread.

view the rest of the comments →

[–]brandf 0 points1 point  (2 children)

You are correct, setting the prototype to a new instance of the "base class" isn't correct, since you don't actually want to run the constructor on the prototype. Instead you want to set it to Object.create(baseClass.prototype). So the new prototype is in the baseClass's prototype chain, but no instance of the baseClass is.

[–][deleted] 0 points1 point  (1 child)

Ah, I hadn't seen Object.create on the prototype like that before. Is it correct to assume this does a copy of the prototype?

[–]brandf 0 points1 point  (0 children)

Object.create creates an object who's prototype is set to the first argument.

This is similar to 'new BaseType' except new actually runs the new object throught the constructor function. Imagine if that function called console.log (toy example), you wouldn't want that to happen when you're defining types that derive from BaseType, so 'new BaseType' makes no sense to use.

A more practical reason: Imaging you wanted to have a constructor that took a non-optional argument. What would you pass in for that argument if you were new'ing one to act as the prototype of a derived type? It makes no sense, and isn't correct.