[Proto-Scripty] Re: Need explanation
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 wrote: > pls explain the above code ,what is does? > > On Thu, Jul 9, 2009 at 5:35 PM, david wrote: > > > Hi Jakir, > > > What is the question ? > > > -- > > david > > > On 7 juil, 12:26, jakir wrote: > > > /* 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; > > > } > > > > };- Masquer le texte des messages précédents - > > > > - Afficher le texte des messages précédents - --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Need explanation
pls explain the above code ,what is does? On Thu, Jul 9, 2009 at 5:35 PM, david wrote: > > Hi Jakir, > > What is the question ? > > -- > david > > On 7 juil, 12:26, jakir wrote: > > /* 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; > > } > > > > > > > > };- Masquer le texte des messages précédents - > > > > - Afficher le texte des messages précédents - > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Need explanation
Hi Jakir, What is the question ? -- david On 7 juil, 12:26, jakir wrote: > /* 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; > } > > > > };- Masquer le texte des messages précédents - > > - Afficher le texte des messages précédents - --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---