1

Say I have two constructors for class Entity, and Player. (Player is intended to inherit from Entity)

var Entity = function(speed) { this.speed = speed; }

var Player = function(name) { Entity.call(this, 10); this.name = name; }

assert(new Player('p1') instanceof Entity === false)

In order for new Player objects to be an instanceof Entity, I would usually do something like...

Player.prototype = new Entity();

But this adds a useless object to the Player prototype chain which i will never use because I don't want players to share a single 'speed' variable. It may be unique for each player.

Player.prototype = Entity.prototype

doesn't work either since Entity objects also become instanceof Player...

Is there a way I can simulate inheritance, just to get instanceof to work, without introducing useless objects in the prototype chain?

Andreas
  • 21,535
  • 7
  • 47
  • 56
Nodeocrat
  • 816
  • 2
  • 14
  • 29
  • wouldn't each instance of `Player` have it's own `speed` in the first case? – Jaromanda X Feb 21 '16 at 11:50
  • Either of the answers below will work (although classes of course require ES6). Note that the new class syntax has the same result as if you defined it the ES5 way; it's just for convenience and doesn't change the way objects are created (i.e. it's just syntactic sugar). – Matt Browne Feb 21 '16 at 11:58
  • 1
    You should have a look at [Benefits of using `Object.create` for inheritance](http://stackoverflow.com/q/17392857/218196) – Felix Kling Feb 21 '16 at 15:50
  • 1
    …and a look at [Why wouldn't I use Child.prototype = Parent.Prototype rather than Child.prototype = new Parent(); for Javascript inheritance?](https://stackoverflow.com/questions/11088365/why-wouldnt-i-use-child-prototype-parent-prototype-rather-than-child-prototype-new-parent) – Bergi Feb 21 '16 at 16:15

2 Answers2

3
Player.prototype = Object.create(Entity.prototype);

This will make a new object with specified prototype (Entity.prototype) and properties.

refer : Object.create()

In ES6, you can use the class-keyword and the extends keyword similar to java.

Ajay Gaur
  • 5,140
  • 6
  • 38
  • 60
1

The ES5 way is to use Object.create(), the ES6 way is to use the class keyword:

class Entity {
  constructor(speed) { ... }
}

class Player extends Entity {
  constructor(name) {
    super(10);
    this.name = name;
  }
}
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • I was using the ES6 class keyword originally, but I don't like having to use 'this.x = ...' all the time to declare properties of new classes, and creating all your properties in just the constructor looks messy. I'll be using [John Resig's](http://ejohn.org/blog/simple-javascript-inheritance/) approach, but first I wanted to understand the old fashioned way of doing it – Nodeocrat Feb 21 '16 at 12:09
  • I really don't see how you can avoid `this` when using constructors and objects in JS... – Madara's Ghost Feb 21 '16 at 12:11
  • You can't, that's why I'm using a different approach. It does use 'this' behind the scenes... but I'm happy so long as it happens behind the scenes. – Nodeocrat Feb 21 '16 at 12:13