8

So I'm learning Javascript and all its' prototype goodness, and I am stumped over the following:

Say I have this

var Animal = function (a, b, c, d, e, f, g, h, i, j, k , l, m, n){
   this.a = a;
   this.b = b;
   //...etc...
};

var x = new Animal(1,2,3....);

Now how do I create a Cat constructor function that inherits from the Animal constructor function such that I don't have to type the super long arguments again?

In other words I don't want to be doing this:

var Cat = function (a, b, c, d, e, f, g, h, i, j, k , l, m, n){
   this.a = a;
   this.b = b;
   //...etc...
};

// inherit functions if any
Cat.prototype = new Animal;

var y = new Cat(1,2,3....);

Thanks in advance! j

John
  • 197
  • 1
  • 8

2 Answers2

9

How's this?

var Cat = Function (a, b, c, d, e, f, g, h, i, j, k , l, m, n){
   Animal.apply(this, arguments);
};

// inherit functions if any
Cat.prototype = new Animal;

var y = new Cat(1,2,3....);
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • "arguments" is a very powerful variable that is automatically in the scope of any function call... look here for more info: http://www.seifi.org/javascript/javascript-arguments.html – Mike Sherov Feb 14 '10 at 23:58
  • 4
    Yuo wouldn't even need to declare the argument list, (a, b, c and so on), in the Cat function. arguments would still contain the values sent in the new Cat(1,2,3...) call. – Lars Blåsjö Feb 15 '10 at 00:10
  • 3
    Careful: the `prototype= new Animal` call instantiates an `Animal`, calling the constructor function with all the arguments set to `undefined`. For a simple constructor that just stores its argument this is acceptable, but for anything more complicated it'll break. Background and (lengthy!) discussion: http://stackoverflow.com/questions/1595611/how-to-properly-create-a-custom-object-in-javascript/1598077 – bobince Feb 15 '10 at 00:21
  • 1
    Also, the Animal.apply bit in it self is not inheritance or making use of prototypes. Animal.apply could be called with any object, also an object that does not inherit from Animal. – Lars Blåsjö Feb 15 '10 at 00:27
  • @Lars#2: So what? It still answers the question. – SLaks Feb 15 '10 at 01:04
  • @Lars, I thought only functions/methods can be inherited using prototypes. How would I do the same with prototypes, so I wouldn't need to redefine the argument list for the constructor function cat? Thanks! – John Feb 15 '10 at 01:19
  • @SLaks, yes it solves the problem, so good suggestion. But on a theoretical level, it is not an inherited constructor, which was also a part of the question. But I'm all for your solution, in practice. – Lars Blåsjö Feb 15 '10 at 18:46
  • @John Not only functions/methods can be inherited, other members also gets inherited, like if you would have Animal.prototype.someValue = 1; in your example, cats would also inherit the someValue member. I don't know about constructors though. Seems hard to inherit the constructor as such, since it is the function you use to define the sub class, like Cat. Maybe someone else knows. – Lars Blåsjö Feb 15 '10 at 18:53
1

It quickly becomes tedious to remember the order and meaning of long lists of parameters like this.

You can add some flexibility if you pass the properties of a new Animal as an object- and it is not so hard to remember as a long list of argument indexes.

function Animal(features){
 for(var p in features){
  this[p]= features[p];
 }
};

You can give every cat some basic cat features automatically, 
and add specifics when you make a new cat.

function Cat(features){
 for(var p in features){
  this[p]= features[p];
 }
}
Cat.prototype= new Animal({legs: 4, eats: 'meat', type: 'mammal', whiskers: true});
Cat.prototype.constructor=Cat;

var Tiger= new Cat({tail: true, hunter: true});
Tiger begins with these properties:

tail: true
hunter: true
legs: 4
eats: meat
type: mammal
whiskers: true
kennebec
  • 102,654
  • 32
  • 106
  • 127