I'm implementing functions in javascript that inherit from a parent class. It works perfectly with this style of code.
function Foo(){}
Foo.prototype = {
constructor: Foo,
name: 'foo',
tostring: function()
{
return this.name;
}
};
function Bar(){}
Bar.prototype = new Foo();
Bar.prototype.name = 'bar';
var b = new Bar();
console.log(b.tostring()); // says 'bar'
This is okay. But I have a lot of properties in Bar and I don't want to repeat Bar.prototype.someProp every time so I used the shorthand version and it isn't inheriting.
function Bar(){}
Bar.prototype = new Foo();
Bar.prototype = {
constructor: Bar,
name: 'bar'
};
console.log(b.tostring()); // doesn't work since the method doesn't exist
I'm assuming Bar.prototype is being overwritten by the native Object of Javascript. How can I inherit using the shorthand Bar.prototype = {}
and avoid repetition?