I have a class declared the traditional way, i.e.
function MyClass() {
}
MyClass.prototype = {
};
Now I want to create a copy of that class (not a copy of the instance the class creates) but change some of the prototype methods. In other words I want to make a copy of the class with some augmentations... do I need to use inheritance for that or I it is enough to loop over and assign references to my new class for the original prototype and the new one?
I would use normal inheritance. Try this:
var MyClass = function(){};
MyClass.prototype = {
foo: function(){ alert('foo') },
bar: function(){ alert('bar') }
};
var MySubClass = function(){};
MySubClass.prototype = new MyClass();
MySubClass.prototype.bar = function(){ alert('otherbar') };
var my = new MyClass();
var mysub = new MySubClass();
my.foo(); // foo
my.bar(); // bar
mysub.foo(); // foo
mysub.bar(); // otherbar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With