It's very hard to put into words what I want, so I've written a JS Example that should help get the point across.
// create animal class
var animal = function(name) {
this.name = name;
};
animal.prototype.getSound = function() {
return this.sound === undefined ? 'nothing' : this.sound;
};
// create animal type classes
var dog = new animal("dog");
dog.prototype.sound = "woof";
var cat = new animal("cat");
dog.prototype.sound = "meow";
var worm = new animal("worm");
// create specific animals
var spot = new dog();
var mittens = new cat();
var squiggles = new worm();
I want to create instances of JavaScript function and extend the prototype multiple times so that, in my example, specific animals have methods and properties of both their genus and the general animal kingdom.
Unfortunately, it appears after the first instancing, the prototype property no longer exists. I don't want to continue extending it so that the main prototype has all the methods of all animals, a worm and dog should be able to have .dig()
, and a cat should be able to have .climb()
without the other classes having those methods.
Conversely, I do not want to constantly rebind methods to each instance. Mittens the cat should have the same prototype as the cat class, and should have the exact same function objects as Crookshanks the cat without any extra assignment work being done.
How do I achieve this with my example?