OOP Javascript Template with constructor and inheritance


/ Published in: JavaScript
Save to your folder(s)

From @blixt http://stackoverflow.com/users/119081/blixt

As you can see, the classes correctly interact with each other (they share the static id from MyClass, the announce method uses the correct get_name method, etc.)

One thing to note is the need to shadow instance properties. You can actually make the inherit function go through all instance properties (using hasOwnProperty) that are functions, and automagically add a super_ property. This would let you call this.super_get_name() instead of storing it in a temporary value and calling it bound using call.

For methods on the prototype you don't need to worry about the above though, if you want to access the super class' prototype methods, you can just call this.constructor.super.prototype.methodName. If you want to make it less verbose you can of course add convenience properties. :)

URL: http://stackoverflow.com/questions/1114024/constructors-in-javascript-objects

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.