Need explanation

6 views
Skip to first unread message

jakir

unread,
Jul 7, 2009, 6:26:51 AM7/7/09
to Prototype & script.aculo.us
/* Based on Alex Arnell's inheritance implementation. */
var Class = {
create: function() {
var parent = null, properties = $A(arguments);
if (Object.isFunction(properties[0]))
parent = properties.shift();

function klass() {
this.initialize.apply(this, arguments);
}

Object.extend(klass, Class.Methods);
klass.superclass = parent;
klass.subclasses = [];

if (parent) {
var subclass = function() { };
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
parent.subclasses.push(klass);
}

for (var i = 0; i < properties.length; i++)
klass.addMethods(properties[i]);

if (!klass.prototype.initialize)
klass.prototype.initialize = Prototype.emptyFunction;

klass.prototype.constructor = klass;

return klass;
}
};


Class.Methods = {
addMethods: function(source) {
var ancestor = this.superclass && this.superclass.prototype;
var properties = Object.keys(source);

if (!Object.keys({ toString: true }).length)
properties.push("toString", "valueOf");

for (var i = 0, length = properties.length; i < length; i++) {
var property = properties[i], value = source[property];
if (ancestor && Object.isFunction(value) &&
value.argumentNames().first() == "$super") {
var method = value;
value = (function(m) {
return function() { return ancestor[m].apply(this,
arguments) };
})(property).wrap(method);

value.valueOf = method.valueOf.bind(method);
value.toString = method.toString.bind(method);
}
this.prototype[property] = value;
}

return this;
}
};

david

unread,
Jul 9, 2009, 8:05:26 AM7/9/09
to Prototype & script.aculo.us
Hi Jakir,

What is the question ?

--
david
> };- Masquer le texte des messages précédents -
>
> - Afficher le texte des messages précédents -

jakkir hussain

unread,
Jul 13, 2009, 3:47:18 AM7/13/09
to prototype-s...@googlegroups.com
pls explain the above code ,what is does?

david

unread,
Jul 14, 2009, 6:01:16 PM7/14/09
to Prototype & script.aculo.us
Hi Jakkir,
the first part of the code introduce a new namespace called "Class"
with one method called "Create".
This portion when called with:
var myClass=Class.create({
initialize:function(myVar){ ... },
myMethod1:function(myVar1){ ... },
myMethod2:function(myVar2){ ... },
...
});
var instance1=new myClass(myParameter); //will create a new instance
of myClass class.
instance1.myMethod1(myParameter1); //will launch the instance1
myMethod method
will create a new Class that will automatically launch the
myClass.initialize method: it's the constructor of the function.

But it also introduce inheritance, so you could create a new
"myNewClass" with the same method as "myClass". And you could also
overwrite "myClass" method in "myNewClass":
var myNewClass=Class.create(myClass,{
initialize:function($super,myNewVar){
...
//when need to call myClass constructor
$super(myNewVar);
....
},
myNewMethod1:function($super,myNewVar1){ ... },
MyNewLocalMethod:function(myLocalVar){ ... }
};
var newInstance1=new myNewClass(myNewParameter);
//this create a new instance of "myNewClass"
//internally, it will call the "myClass" constructor.
newInstance1.myNewMethod1(myParameter1);
//internally, it can call myMethod1 of "myClass".
newInstance1.MyNewLocalMethod(myLocalParameter1);
//method is only avalaible to the newInstance1 and not to instance1


The second part of the code is a extention that will added to you
instance so that you could extend properties of the instance created.

This a quite complicated portion of code wich need more than a thread
to be fully explained.
btw, I hope it demistify a little bit this code; if not, check:

for more information, first reed prototype API doc:
Class.create: http://prototypejs.org/api/class/create
addMethods: http://prototypejs.org/api/class/addMethods

you can also look at:
http://www.webreference.com/js/column79/index.html
http://ejohn.org/blog/simple-javascript-inheritance/#postcomment
--> also check comments for this post
http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/


--
daivd

On 13 juil, 09:47, jakkir hussain <jakkir.sha...@gmail.com> wrote:
> pls explain the above code ,what is does?
>
Reply all
Reply to author
Forward
0 new messages