Here are some defineClass' advantages as I see them:
1. defineClass implements type linearization <http://goo.gl/f0cQU> (don't
confuse it with multiple inheritance. There are no ambiguities) borrowed
from Scala <http://goo.gl/vqHDe> programming language. Traits are
applied one by one, overriding existing methods and customizing the
behavior. It allows extracting different aspects to separate traits and
reuse them in different class hierarchies. As per MooTools docs, its
"Implements" thing just copies the properties from listed classes without
inheriting them properly.
Here is an example of an aspect extracted to a separate trait:
// this trait adds logging aspect to a method called "bar"
var LoggedBarTrait = {
bar: function () {
console.log("Starting bar");
this._super(); // call the overridden method
console.log("Finished");
}
};
// just a class with a method bar
var Foo = defineClass({
bar: function () {
// ...
}
});
// the same class with logging imported
var Foo2 = defineClass({
_super: [Foo, LoggedBarTrait],
// ...
});
Here the Foo2 class imports the logging aspect by applying the trait.
The trait can be reused in other class hierarchies. Multiple traits can
applied even if they customize the same method(s). The order of traits is
important because it control the trait application order.
Read more on type lineraization here <http://goo.gl/f0cQU>.
2. Nested classes <http://goo.gl/cFzPP> can be overridden in a subclass,
so they are as flexible as virtual methods. By implementing a functionality
as a class rather than a monolithic method you can decrease its
implementation complexity.
Example:
var Foo = defineClass({
// define a nested class
Bar: defineClass({
qux: 1,
compute: function () {
return this.qux * 2;
}
}),
compute: function () {
return new this.Bar().compute();
}
});
assert(new Foo2().compute() === 2);
// override the nested class
var Foo2 = defineClass({
_super: Foo,
Bar: {
qux: 2
}
});
assert(new Foo2().compute() === 4);
3. Some people care about code size.
4. Not really an advantage, but purists may agree. I believe good PLs
and core libraries reserve/introduce as less keywords/aspects as possible.
MooTools reserves "initialize", "Extends", "Implements" and "parent",
whereas defineClass reserves only single one: "_super". I don't consider
the "constructor" as a special defineClass' notation because every
prototype has it.
Thanks,
-Nodir
On Wed, May 23, 2012 at 12:53 AM, Duncan Gmail
<[email protected]>wrote:
> Not sure about using OOP instead of functional.
> But leaving that aside, what differentiates this from the Server-side
> MooTools?
>
> On 22 May 2012, at 18:07, Jeff Barczewski <[email protected]>
> wrote:
>
> Nodir,
>
> For OOP work, your defineClass looks really nice.
>
> I need to take a closer look but from first glance I like what you have
> created.
>
> Thanks for sharing!
>
> Jeff
>
> On Monday, 21 May 2012 21:25:33 UTC-5, Nodir Turakulov wrote:
>>
>> If anybody is still interested in OOP, I've made an npm
>> package<http://search.npmjs.org/#/defineClass>that provides:
>>
>> 1. Class inheritance
>> 2. True method overriding
>> 3. Proxy class generation
>> 4. Scala-like traits (Ruby-like mixins)
>> 5. Nested classes (with overriding in subclasses).
>>
>> Here is an example of usage:
>>
>> var Person = defineClass({ cash: 0,
>> constructor: function (firstName, lastName) {
>> this.firstName = firstName;
>> this.lastName = lastName;
>> },
>>
>> greet: function (name) {
>> console.log("Hello " + name + ". My name is " + this.firstName);
>> } });*var* Developer = defineClass({ _super: Person,
>>
>>
>> // override a field default value
>> cash: 100,
>>
>> // override the constructor
>> constructor: function (firstName, lastName, language) {
>> // you may have code before the base constructor call
>>
>> // call the base constructor
>> this._super(firstName, lastName);
>> this.language = language;
>> }
>>
>> // override a method
>> greet: function (name) {
>> console.log("Hey, " + name + ". I'm " + this.firstName)
>> },
>>
>> // override a method and call its base method
>> earn: function (amount) {
>> return this._super(amount * 1.2);
>> }});
>>
>> More in the readme:
>> https://github.com/**nodirt/defineClass<https://github.com/nodirt/defineClass>
>>
>> Installation:
>>
>> $ npm install defineClass
>>
>> -Nodir
>>
>> On Friday, May 6, 2011 12:07:47 PM UTC+5, Thierry Templier wrote:
>>>
>>> Hello,
>>>
>>> I wonder what is the best / recommended approaches and/or JS libraries
>>> to use in order to implement OOP within node.js applications.
>>>
>>> Thanks very much for your help!
>>> Thierry
>>
>> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en