I am tinkering with prototypal inheritance in JavaScript.
I have the code below and I am trying to find out if there is a way of getting the code which makes Cat inherit Animal actually inside the Cat function - so that it is all niceley encapsulated as one unit of code.
The line in question is:
Cat.prototype = new Animal();
Any ideas if this is possible?
<script>
function Animal()
{
function privateFunction()
{
alert("I m private");
}
function breathe()
{
alert("I am breathing");
}
function eat()
{
alert("I am eating");
}
return {
breathe: breathe,
eat: eat
};
}
function Cat(name, age)
{
this.name = name;
this.age = age;
}
Cat.prototype = new Animal();
var joey = new Cat("Joey", 5);
joey.breathe();
joey.eat();
</script>