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,
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