4

I have a constructor Monkey():

function Monkey(name, age) {
    this.name = name;
    this.age = age;
}

I want to make another constructor named Human() with an extra property cars which will store number of cars the person has along with all the property that Monkey has (like name and age)

I don't want to repeat all the Monkey stuff in the new Human stuff. Is is possible to clone the Monkey and extend a property with prototype?

3 Answers 3

7

I've tried this code, I guess it's what you want:

function Human(name,age,cars){
    Monkey.call(this,name,age);
    this.cars = cars;
}

This way, the Human constructor calls the Monkey constructor as a normal function, but setting its namespace as the new Human object. Thus, in this case, the this keyword inside Monkey constructor refers to a object of class Human, and not Monkey. Also, with this code, the condition new Human() instanceof Human; returns true, since I'm not returning a new instance of Monkey, just using its constructor.

Also, you can "clone" the prototype, as you said. Just do this:

Human.prototype = Monkey.prototype;

EDIT

As @Bergi amd suggested, the best way to clone a prototype is using the Object.create method, as follows:

Human.prototype = Object.create(Monkey.prototype, {constructor:{value:Human}});
Sign up to request clarification or add additional context in comments.

6 Comments

Human.prototype = Object.create(Monkey.prototype, {constructor:{value:Human}}); would be better, else all monkeys are instanceof Human and (new Human).constructor would be monkey.
@Bergi Thanks, I didn't know about it. I will update the answer
Actually, you should do both - then you get the methods on the prototype and the side-effects of the constructor. (Or what @Bergi said, but if Object.create isn't supported, then it's Human.prototype = new Monkey(); Human.prototype.constructor = Human;.)
@minitech: I'm sure it was meant to be both. Btw, you should not use Human.prototype = new Monkey - you're getting the side effects of the constructor where they are not applicable. There are better ways to shim Object.create
@Bergi: Yeah, I just meant in this particular case, since the constructor doesn't do anything except set properties.
|
0

I'm writing this answer simply to complement the others, it SHOULD NOT be used unless you fully know the impact of using the non-standard __proto__.

function Monkey(name, age) {
    this.name = name;
    this.age = age;
}

function Human(name, age, cars) {
  this.__proto__ = new Monkey(name, age);
  this.cars = cars;
}

console.log(new Human(1, 2, 3));

See also

4 Comments

Actually I don't think it should be used at all - the inheritance is weird :-)
@Bergi You mean the fact that humans inherit from monkeys? I agree :) Seriously though, the prototype structure doesn't strike me as unintuitive.
No. Every single human inherits from its own monkey. No human is instanceof Human, and Human.prototype is useless.
@Bergi That makes sense; but that's also due to the nature of the question itself in a way.
0

Simple start for functional style/parasitic'ish inheritance:

function Human(name, age, cars) {
  var that = new Monkey(name, age);
  that.cars = cars;
  return that;
}

As outlined by Douglas Crockford

http://www.crockford.com/javascript/inheritance.html

9 Comments

Yes, but this way the constructor returns an object of type Monkey, and not of Human (see new Human() instanceof Human;)
@MarkLinus "type"? Sounds like you're pulling classical ideas into a far more dynamic space. Relying on things like instanceof just bind you to misaligned paradigms in JavaScript.
I'm just saying that, though it's correct, your solution don't solves this problem I described: new Human()will not be an instance of class Human as expected
@MarkLinus You're answering with bias a question that wasn't asked. Using instanceof like that is a form of programming to implementation. Both objects provide the same functionality, and this solution begins by saying it is leaning in the functional direction which implies the object has more in common with data than a type system.
I know it wasn't, but this might be a problem in the future, since I don't know how the OP will use this class
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.