I want to create two functional classes in Javascript: Animal and Zebra. The client script is to instantiate Zebra, and Zebra should then be able to see and call functions from Animal:
Zebra : Animal
Thus, I tried the following, where I use the jQuery $.extend() to make Animal a base class to Zebra:
Animal = function() {
function hi_animal() {
console.log('But I am also an animal');
}
return {
hi_animal: hi_animal
}
}
Zebra = function() {
$.extend(this, Animal);
function hi_zebra() {
console.log('I am a zebra!');
hi_animal();
}
return {
hi_zebra: hi_zebra
}
}
$(document).ready(function() {
var my_zebra = new Zebra();
my_zebra.hi_zebra();
});
The browser log should show these two lines:
I am a zebra
But I am also an animal
However, I only see:
I am a zebra!
Uncaught ReferenceError: hi_animal is not defined
Here is a fiddle.
What am I missing?