tag:blogger.com,1999:blog-22587889.post5865585755386764776..comments2024-02-11T13:21:47.930+05:30Comments on Ruminations of a Programmer: I didn't know Javascript can do this !Anonymoushttp://www.blogger.com/profile/01613713587074301135noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-22587889.post-2388302236447675742007-10-12T10:10:00.000+05:302007-10-12T10:10:00.000+05:30@Roberto: Thanks for your feedback. Just a quick p...<I>@Roberto:</I> Thanks for your feedback. Just a quick point on inheritance - inheritance enforces stronger coupling between classes and hence may not be the best solution in this case. Closures provide a more natural way of implementing strategy like design pattern. For more on the evils of inheritance, have a look <A HREF="http://ajaxian.com/archives/inheritance-is-evil-and-must-be-destroyed-part-1" REL="nofollow">here</A>.Anonymoushttps://www.blogger.com/profile/01613713587074301135noreply@blogger.comtag:blogger.com,1999:blog-22587889.post-2671770639871603882007-10-12T05:10:00.000+05:302007-10-12T05:10:00.000+05:30HeyyI see you like to make things look like java, ...Heyy<BR/>I see you like to make things look like java, so with the code in the bottom of this comment you can define classes and make use of inhetihance like this:<BR/><BR/>var Clazz = Class.create({<BR/> prop1 : null,<BR/> propX : "BLABLABLA",<BR/><BR/> initialize : function(){<BR/> // constructor<BR/> }<BR/><BR/> method1 : function(){<BR/> // TODO Impl<BR/> }<BR/><BR/> // etc<BR/>});<BR/><BR/>var SubClazz = Class.extend(Clazz, {<BR/> prop1 : "OVERRIDED",<BR/><BR/> method1 : function(){<BR/> // js is fun<BR/> alert(propX); // BLABLABLA<BR/> alert(prop1); // OVERRIDED<BR/> }<BR/>});<BR/><BR/>/******************************/<BR/>gOT IT????<BR/>/******************************/<BR/><BR/>tHE MAGIC CODE TO MAKE THIS POSSIBLE:<BR/><BR/>/**<BR/> * Implements object extendability in the Object definition.<BR/> * @param {Object} destination The code implementing the extensions.<BR/> * @param {Object} source The source to be extended.<BR/> */<BR/>Object.extend = function(destination, source){<BR/> for (var property in source)<BR/> destination[property] = source[property];<BR/><BR/> return destination;<BR/>}<BR/><BR/>/**<BR/> * Definition of a Class.<BR/> * Represents the OOP concepts of a Class.<BR/> */<BR/>var Class = {<BR/> /**<BR/> * Defines and creates a new Class instance.<BR/> * <BR/> * @param {Object} impl The implementation of the class being created.<BR/> * @return {Object} The class defined.<BR/> */<BR/> create: function(impl){<BR/> // TODO THROW NEW EXCEPTION IF IMPL NOT VALID<BR/> var clazz = function(){<BR/> // TODO IMPLEMENT EXCETION CATCHING FOR LOGGIN, AS WELL AS THE LOGGIN COMMANDS IT SELF<BR/> if (this.initialize) this.initialize.apply(this, arguments);<BR/> else if (this.superClass.initialize) this.superClass.initialize.apply(this, arguments);<BR/> };<BR/> clazz.prototype.superClass = Object.prototype;<BR/> if (impl && (typeof(impl) == "object")) clazz.prototype = impl;<BR/> return clazz;<BR/> },<BR/><BR/> /**<BR/> * Defines and creates a new Class instance based on a given SuperClass.<BR/> * Represents the OOP concept of inheritance providing a way of defining a SubClass.<BR/> * <BR/> * @param {Object} source The Class to be extended.<BR/> * @param {Object} additions The Subclass implementation.<BR/> * @return {Obejct} the subclass defined.<BR/> */ <BR/> extend: function(source,additions){<BR/> // TODO THROW NEW EXCEPTION IF IMPL NOT VALID<BR/> var clazz = Class.create();<BR/><BR/> if (source.prototype) Object.extend(clazz.prototype ,source.prototype);<BR/> Object.extend(clazz.prototype, additions);<BR/><BR/> clazz.prototype.superClass = source.prototype;<BR/> return clazz;<BR/> },<BR/><BR/> /**<BR/> * Defines a static Class.<BR/> * Represents the OOP concept of a static Class.<BR/> * It actualy returns an object instance of the class defined.<BR/> * <BR/> * @param {Object} impl The Class implementation.<BR/> * @return {Object} The static class defined.<BR/> */ <BR/> createStatic: function(impl){<BR/> var clazz = this.create(impl);<BR/> return new clazz();<BR/> }<BR/>}<BR/><BR/><BR/>hope you like this!<BR/>hlobo@inf.ufsc.brAnonymoushttps://www.blogger.com/profile/03165160451963348604noreply@blogger.comtag:blogger.com,1999:blog-22587889.post-52786985026501997282007-10-11T01:49:00.000+05:302007-10-11T01:49:00.000+05:30Dude Web 2.1!http://en.wikipedia.org/wiki/Web_2.1Dude Web 2.1!<BR/>http://en.wikipedia.org/wiki/Web_2.1Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-22587889.post-81808387500556943082007-10-10T21:51:00.000+05:302007-10-10T21:51:00.000+05:30@Stuart: I am learning Javascript, hence I may be ...<I>@Stuart:</I> I am learning Javascript, hence I may be wrong. But let me clarify my understanding ..<BR/><BR/>In the example, this.taxCalc is privileged in the sense that Crockford describes <A HREF="http://www.crockford.com/javascript/private.html" REL="nofollow">here</A>. A privileged method is one that is able to access the private variables and methods, and is itself accessible to the public methods and the outside.<BR/><BR/>The closure allowanceCalc is private static as described <A HREF="http://www.litotes.demon.co.uk/js_info/private_static.html" REL="nofollow">here</A>.Anonymoushttps://www.blogger.com/profile/01613713587074301135noreply@blogger.comtag:blogger.com,1999:blog-22587889.post-15181323746520827692007-10-10T19:52:00.000+05:302007-10-10T19:52:00.000+05:30Interesting notes...I'm a bit confused though, abo...Interesting notes...<BR/><BR/>I'm a bit confused though, about your comments for the methods in your WageCalc class.<BR/><BR/>this.taxCalc is noted as privileged, however anyone can call it, change it, or override it.<BR/><BR/>the allowanceCalc also (IIRC) needs a 'var' before it, otherwise in JavaScript it is a global method on the window, thus not private in any way.<BR/><BR/>var allowanceCalc = function(){<BR/> // 50% of basic wage<BR/> return 0.5;<BR/>};<BR/><BR/>With the 'var' declaration, it is scoped to the WageCalc Class, and can't be directly called like...<BR/><BR/>var allowanceCalcValue = new WageCalc(1000).allowanceCalc();Anonymousnoreply@blogger.com