I'm not an expert JS coder/frontend engineer, but I'd love to be one someday. Maybe for some smaller programs, modular programs with split apart functions might work. But alas, this may not be the case in larger scale programs. I noticed this while working on my senior project.
Things may be nice and good on the small scale, but as they get larger, it gets less nice. It doesn't get nice at all. The problem is that sometimes one writes functions that have nothing to do with the overall program, but act like "helper" functions to other functions ... or "metafunctions". It is at cases like these that one could make use of encapsulation provided by object-oriented programs. This and other reasons might be inspirations to adapting OO JS.
Classes in Mootools
The official Mootools documentation provides a great introduction to the Class object in their framework. This post is more like a repitition, while stressing on why we need OO JS.
Before digging in, lets take a look at what we need out of OOP. Personally, what I need is a simple way to define classes, support for constructors and overloading, and inheritance. I skipped out association and aggregation because it seems by default, since one of the members of the Class could be another object .. which is sufficient to achieve the two. We will now take a look at the above features in MooTools
Defining a class
One can define a new class as follows
Adding default constructor
Overloading is a JS thing, not mootools:
I can't think of a cleaner way of doing this, so if someone can, please inform me.
Let's say we want to inherit the class person by another Class, Student. We use the keyword Extends, which "should be the first property in a class definition."
Adding other functions
Adding other methods (functions to the class) is rather simple:
For more details, consult their official documentation. If you have any comments about the tutorials, please say them bellow. Happy coding !
The people have spoken
comments powered by Disqus