I want to make simple classic inheritance with JavaScript. I just need sub classing and method overriding and not the verbose syntax and bells and whistles as provided by prototype.js or some of the other libraries.
Now, this fellow named Shelby S. Moore has come up with a solution that works just the way I want it to:
http://www.coolpage.com/developer/javascript/Correct%20OOP%20for%20Javascript.html
Only problem is that he is extending native types Object and Function which breaks some of the libraries I use. Also as a general observation I don’t want to mess with the prototypes of native objects.
I’ve made Shelby S. Moore’s example live here:
http://jsfiddle.net/christian1974/CEKL5/
As you can see from the example it works as expected.
Now, the $64.000 question is: Can you recommend a way of making it work without messing with Object.prototype and Function.prototype?
I was looking for a really simple syntax like:
Extend(parent, this);
Should I just drop the whole idea and go with an existing library that does this? Am I making life too difficult for my self?
Why not, instead of augmenting the object prototype, just create a function
inherits?That being said, I don’t really see the benefits of this method over, say,
Object.createand -since you’re using libs- jQuery’s.extendmethod