0

Why do some people use

subclass.prototype = Object.create(superclass.prototype);

and others

subclass.prototype = superclass.prototype;

One uses the class while the other creates an instance of the class before attaching it to the prototype of the subclass. Which one is the correct way to do inheritance?

Brian Howell
  • 67
  • 2
  • 13
PMat
  • 2,039
  • 2
  • 29
  • 46
  • See this article that goes over `Object.Create` - [Object.create(): the New Way to Create Objects in JavaScript](http://www.htmlgoodies.com/beyond/javascript/object.create-the-new-way-to-create-objects-in-javascript.html) – Igor Jun 09 '16 at 19:39
  • @Bergi just saw my mistake, I deleted the comment – nem035 Jun 09 '16 at 19:47

1 Answers1

1

The first one is correct. The second one means that the two "classes" have the same prototype chain, so superclass won't even appear in the prototype chain for instances of subclass.

Paul
  • 139,544
  • 27
  • 275
  • 264