0

So I have this code which inherits :

enemy = new Object(hawk);

The parent is this:

function CreateEnemy(name, health, damage){
    this.name = name;
    this.health = health;
    this.damage = damage;
}

var hawk = new CreateEnemy("Hawk", 200, 20);

Whenever I do

enemy.health -= 10;

It changes the value of hawk.health as well, and I don't want it to do that.

scunliffe
  • 62,582
  • 25
  • 126
  • 161
ECMAScript
  • 4,439
  • 4
  • 21
  • 29
  • 3
    I do not see any inheritance or constructing a new object with `enemy = new Object(hawk);` - the outcome actually is `enemy === hawk`. What do you intend to do? – Bergi Sep 13 '13 at 12:38
  • I think you're missing a word in the title, but I can't figure out what it is. – Dennis Sep 13 '13 at 12:38
  • It seems that you wish to clone the object - passing an object to `new Object()` [will **not** clone it](http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.2). – Qantas 94 Heavy Sep 13 '13 at 12:39
  • Perhaps you were thinking of `var enemy = Object.create(hawk);` instead. – Pointy Sep 13 '13 at 12:39
  • 1
    Btw, it's odd to let a constructor begin with `Create`. You'd either expect `new Enemy()` or `createEnemy()` (or maybe `Enemy.create()`) – Bergi Sep 13 '13 at 12:44
  • If you ended up with this code because you've noticed that instance specific properties were shared for child instances when you use `Child.prototype=new Parent()` than you can read this: http://stackoverflow.com/a/16063711/1641941 It'll cover basic inheritance with JavaScript constructor functions. – HMR Sep 14 '13 at 01:39

2 Answers2

2

I think you're looking for

CreateEnemy.prototype.clone = function() {
    return new CreateEnemy(this.name, this.health, this.damage);
};

then do

var enemy = hawk.clone();

Now changes on one object will not reflect upon the other, as they are distinct CreateEnemy instances.


If you are looking for prototypical inheritance, Object.create will do it. Changes on hawk will then be reflected on enemy as long as the properties are not overwritten there:

var enemy = Object.create(hawk);
enemy.name; // "Hawk" - inherited
enemy.damage = 25; // assignment creates an own property on enemy
enemy.damage; // 25
enemy.health -= 10; // as does when getting an inherited value before:
enemy.health; // 190
hawk.health; // 200
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Could you explain how the clone works? Am I suppose to replace the function CreateEnemy() that I had originally with that? – ECMAScript Sep 13 '13 at 13:05
1

In this situation, CreateEnemy is not a parent object, it is a function that is creating an object hawk which you have previously made equal to the object called enemy. Hence, their properties are equal.

DevlshOne
  • 8,357
  • 1
  • 29
  • 37
  • Thank you! So if I do enemy = object.create(hawk) that will be inheritance? – ECMAScript Sep 13 '13 at 12:44
  • Use the `clone` method that **Bergi** mentions above and you can create as many different predatory birds as you like without them sharing properties. – DevlshOne Sep 13 '13 at 12:46