Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a constructor function to inherit from a constructor function in Javascript?

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

like image 329
John Avatar asked Feb 14 '10 23:02

John


People also ask

Are constructors inherited in JavaScript?

We can observe that the constructor function of Employee is inherited to create a new constructor function Developer which can be used to create objects with new properties along with the inherited properties of the parent constructor.

Is it possible to use function constructor to create a new function in JS?

The Function() constructor creates a new Function object. Calling the constructor directly can create functions dynamically, but suffers from security and similar (but far less significant) performance issues as eval() .

How do we invoke a constructor function?

Constructors are invoked automatically when we create objects. Constructors should be declared in the public section of the Class. Constructors cannot return values to the calling program because they do not have return types. Constructors should always be non-virtual.

How do constructor functions work in JavaScript?

A constructor is a special function that creates and initializes an object instance of a class. In JavaScript, a constructor gets called when an object is created using the new keyword. The purpose of a constructor is to create a new object and set values for any existing object properties.


2 Answers

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....);
like image 165
SLaks Avatar answered Sep 19 '22 22:09

SLaks


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
like image 41
kennebec Avatar answered Sep 19 '22 22:09

kennebec