I was wondering, given the limitations of javascript's prototypal inheritance, would it be possible to emulate class-based inheritance as seen in other OOP languages.
I've created a superclass and subclass as follows:
//Parent
function Animal(i)
{
//Some constructor implementation
}
//Child
function Pig(i)
{
//Some additional constructor implementation
}
Pig.prototype = new Animal();
Can I ensure that the child inherits its parent's functions in such a way that I do not need to explicitly call them, or create them in the child object?
Animal.prototype.eat = function()
{
console.log("eating...");
}
Pig.eat(); //No implementation of eat in the Pig object
Can I ensure that the child inherits all of its parent's object variables in addition to its own ones without explicitly calling the parent's constructor like this:
function Pig(i)
{
User.call(this, i);
//Some pig-like variables
}
Basically, I'd like to create all the implementation in my parent class and only write functions that need to be overridden + any additional functions in the child class. If I want to call Pig.eat(), I'd like it to use the parent function if one doesn't exist in the child object. And upon creating a Pig object, I'd like it to inherit its parent's variables in addition to its own unique ones. Is this possible?