>>>>3
>>>>As first, to keep the compatibility with MooTools.
>>If that's your objective then good luck. But it doesn't describe the instance objects as clearly as a Java or C++ class definition does.
It is my objective, only for the basic features provided of my function...! :)
About Implements, i have read more than one post about devs asking how to use it so i think it is considered quite confusing from many sources and i am not saying it is not; i was confused too when i used MooTools for the first times.
Anyway i think Implements has a lots of power. Well, i don't want to convince anyone about that, it is only my opinion and i think every dev probably has a different opinion about it...
I wrote an example about Implements at the end of my answer; i hope i didn't write something stupid or wrong and i hope it could explain my point of view and maybe it could be useful.
>>>>4
>>>>The Class function takes an object as input parameter, that object is
>>>>the prototype of your future class.
>>It shouldn't be a prototype, or described as one. It should be a
>>declaration of what must be in each object. E.g A collection of data
>>property names; a collection of method property names and their
>>definitions; odds and ends such as super's name.
Well, as i wrote, the prototypal chain is "broken" so, in the end, i think the Class's parameter looks like a "declaration of what must be in each object" and we could say "it is"... You know it is the prototype only checking the prototype of your class and i think it is really important to get members on the prototype of the class: thank to that, you can easily inherit your class using prototypal inheritance.
>Incidentally, why "initialise"; why not "construct"?
To keep MooTools compatibility! :)
As i wrote in my post: "(... my function ...) is intended to be a really light copy of the MooTools’s Class function with basic features"
An example about Implements:
----------------------------
Preamble:
You have objects ObjectA and ObjectB; you wrote a Class Reader that reads some data (as text) from a source, process it and return the result.
You want to add the Reader feature on those objects but that feature needs to access the data from those objects; one object has the data stored on its this.text property and the other one on the this.otherText property.
You don't want to change the objects's code... you are using them in so many places yet!
This is a really clean way in which i think you can solve the problem:
var DIE = function() { throw new Error("Method not implemented"); };
var Reader = new Class({
read: function() {
var data = this.getData();
data = this.convert(data);
return data;
},
convert: function(data){
// some operations on data...
return data + ";";
};
getData: function(){
DIE();
}
});
var ObjectA = new Class({
Implements: Reader,
text: "text",
/*some more members if you want...*/
getData: function(){
return this.text;
}
});
var ObjectB = new Class({
Implements: Reader,
otherText: "other text",
/*some more members if you want...*/
getData: function(){
return this.otherText;
}
});
var a = new ObjectA(),
b = new ObjectB();
alert(a.read()); // alert "text"
alert(b.read()); // alert "other text"
Add the "read" feature on objects with potentially completely different members was really clean!
The same in pure Js is:
var Reader = function() {};
Reader.prototype.read = function() {
var data = this.getData();
data = this.convert(data);
return data;
};
Reader.prototype.convert = function(data) {
return data + ";";
};
Reader.prototype.getData = function() {
DIE();
};
var ObjectA = function() {};
ObjectA.prototype.data = "text";
/*some members*/
ObjectA.prototype.getData: function(){
return this.data;
};
for(var fn in Reader) {
ObjectA.prototype[fn] = Reader.prototype[fn];
}
var ObjectB = function() {};
ObjectB.prototype.otherData = "other text";
/*some members*/
ObjectB.prototype.getData: function(){
return this.otherData;
};
for(var fn in Reader) {
ObjectB.prototype[fn] = Reader.prototype[fn];
}
var a = new ObjectA(),
b = new ObjectB();
alert(a.read()); // alert "text"
alert(b.read()); // alert "other text"
You can do the same in Js, no additional code and less operation executed then...
But to me the Implements version is really more readable and that makes the difference for me.
I wrote a lots, sorry about that...
All i wrote is my modest opinion.