I'm trying to teach myself javascript inheritance.
I have created the following code and try to make it work (After reading some related posts around here).
I must use cross browser syntax and want to use only pure js.
How can I fix the following code?
Update: (After Bergi remarks, Now fully working...)
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Tests</title>
<script type="text/javascript">
function Animal(name)
{
alert("animal ctor " + name);
this.animalName = name;
};
//Base implementation
Animal.prototype.move = function()
{
alert("I am walking");
};
//Base implementation
Animal.prototype.makeVoice = function ()
{
alert("Not implemented");
};
Animal.prototype.introduce = function ()
{
alert("This is " + this.animalName);
};
function Bird(name)
{
alert("bird ctor " + name);
Animal.call(this, name);
//Derived only
this.standOnWire = function ()
{
alert("I'm standing on an electricity wire");
};
};
Bird.prototype = new Animal();
Bird.prototype.constructor = Bird;
//Override
Bird.prototype.move = function()
{
alert("I am flying");
};
//Override
Bird.prototype.makeVoice= function ()
{
alert("twit twit");
};
function Cow(name)
{
alert("cow ctor " + name);
Animal.call(this, name);
//Derived only
this.produceMilk = function ()
{
alert("I am producing milk");
};
};
Cow.prototype = new Animal();
Cow.prototype.constructor = Cow;
//Override
Cow.prototype.makeVoice = function ()
{
alert("mmmooooooo");
};
function runCowTests()
{
alert('runCowTests');
var cow1 = new Cow('Cow');
cow1.introduce();
cow1.move();
cow1.makeVoice();
cow1.produceMilk();
};
function runBirdTests()
{
alert('runBirdTests');
var bird1 = new Bird('Canary');
bird1.introduce();
bird1.move();
bird1.makeVoice();
bird1.standOnWire();
};
</script>
</head>
<body>
<input type="button" value="bird tests" onclick="runBirdTests();" />
<input type="button" value="cow tests" onclick="runCowTests();" />
</body>
</html>