Hello,
After looking for a simple Class system in JavaScript, and looking at all the horrible libraries, forced upon terrible syntax, cluttered objects and prototypes... I'd like to introduce a simple alternative.
This is a very bare-bones, and yet one of the more powerful class systems in JavaScript. Built on-top of Object.create
and prototype
inheritance using the methods that everyone suggests is the best way to do classes in JavaScript ported over to a nicer syntax.
It doesn't clutter the properties or the prototype
aside from a single method which is extends
, and even this method is sugar and not required!
Check out zoo.js
for an in-depth example of how to use Class
. No matter how deep down the rabbit hole you go, it should keep references so you can do instanceof
checks on children of a parent of a grand-parent and so forth.
Here we can see that you still have <anon>()
methods (don't worry, the reference to do instanceof
checks still exists, the naming just looks bad when graphed) which is hard to get around when sugaring the prototype
syntax. Trust me, I tried to avoid it.
If you can find a simpler way, let me know and I'll update it.
If you want to throw in events this works great with: https://gist.github.com/Nijikokun/7298768 just add
Class.observe = observe function here
beforeClass.chain
and inside of the invoking method doClass.observe(this)
and you are done.