This page describes how Private Members in Javascript could be implemented: http://www.litotes.demon.co.uk/js_info/private_static.html#nInhe As said in http://www.prototypejs.org/learn/class-inheritance the prefered way to create classes in prototype is: var Person = Class.create({ initialize: function(name) { this.name = name; }, say: function(message) { return this.name + '': '' + message; } }); So how do I add PrivateMethods and Members here? Is there an elegant way or do I have to implement something like that myself? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
No iedea? I thought this could be a common problem ... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Dean79 wrote:> This page describes how Private Members in Javascript could be > implemented: > > http://www.litotes.demon.co.uk/js_info/private_static.html#nInhe > > As said in http://www.prototypejs.org/learn/class-inheritance > the prefered way to create classes in prototype is: > > var Person = Class.create({ > initialize: function(name) { > this.name = name; > }, > say: function(message) { > return this.name + '': '' + message; > } > }); > > So how do I add PrivateMethods and Members here? > Is there an elegant way or do I have to implement something like that > myself? >The traditional way to add private properties and methods in JavaScript is to wrap your code in an anonymous function. Below is one example. - Ken Snyder var Person = (function() { // private property var privateName; // private method var isValidMessage = function(message) { return /^[\w .?]+$/.test(message); }; // public properties + methods return Class.create({ initialize: function(name) { privateName = name; this.publicVar = 1; }, say: function(message) { if (isValidMessage(message)) { return privateName + '': '' + message; } else { throw ''Invalid message''; } } }); })(); --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Again, I want to use the prototype Class.Create feature, because of the inheritance implementation. The Problem with this procedure of wrapping the class.create code in an anonymous function, is that that these private members are also private if the super method is called from the inherited class. var BaseClass = (function() { var _myPrivateMember = 5; return Class.create( { initialize: function(id) { }, // add public methods here myPublicMethod: function() { alert(_myPrivateMember); } }); })(); // inherit from BaseClass var InheritedClass = (function() { return Class.create(BaseClass, { initialize: function() { }, // override, from base class myPublicMethod: function($super) { // do some stuff $super(); // calls method from super class, but the superclass // method does not know its privat member any more } }); })(); If I call myPublicMethod from the inherited class, it tries to call the method of the baseclass. Unfortunetaly the Method of the baseclass doesn''t know the privatmember anymore. That is, why I was asking, if their is a best practice method for private methods with inheritance in prototype. I would really appreciate if their was a solution for this :( cheers Dean --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Dean, Have you tried executing your code? There shouldn''t be any problem with it as the reference to _myPrivateMember is still intact, even through inheritance. Your private member could even be a function and it would still work. The pattern that I''ve settled on with which I currently prefer for using Class.create() with private and public members is something like the following: MyClass = Class.create((function() { var _private = { aPrivateFunction: function(param) { /* do something here */ }, aPrivateVariable: 100.1 }; var _public = { initialize: function() { _private.aPrivateFunction(_private.aPrivateVariable); }, aPublicFunction: function() { /* do something interesting */ } }; return _public; })()); Of course there are plenty of variations on this and you should do whatever is most readable, and therefore maintainable, for you. A popular choice is to ditch the object _private and just declare each function or variable separately before returning the public object, and you don''t need to declare _public either, you could just return it, you''ll see this kind of thing in the prototype source I think. This pattern is fine with inheritance, your private members won''t be accessible to either the outside of the class or any subclasses (that''s the point of making them private) but the subclasses will still work properly because their superclass still maintains references to them. One thing I like about using a _private object is that I can perform some handy operations on it. For example, if I want to use ''this'' in my private objects then this can be achieved by binding them when the class is initialised and I have this handy nugget in my prototype extensions just for the job: Class.thisify = function(me, hash) { $H(hash).each(function(pair) { if (Object.isFunction(pair.value)) { hash[pair.key] = pair.value.bind(me); } }); }; So, extending the above example I can do things like this: MyThisifiedClass = Class.create((function() { var _private = { aPrivateFunction: function(param) { this.aPublicFunction(); }, aPrivateVariable: 100.1 }; var _public = { initialize: function() { Class.thisify(this, _private); _private.aPrivateFunction(_private.aPrivateVariable); }, aPublicFunction: function() { /* do something interesting */ } }; return _public; })()); Comes in very handy and it''s so nice to be able to have objects with fully functional private members. I''d love private members to be integrated into Prototype, I think I remember reading something about one of the other majors implementing private members by interpreting members starting with underscores as being private. Class.create() could do this, any property in the object starting with an underscore could be kept private, everything else could be public as normal. -- Rod On Apr 8, 7:14 pm, Dean79 <d...-/ZcioKyrMXknxqbYAscKCQ@public.gmane.org> wrote:> Hi Again, > > I want to use the prototype Class.Create feature, because of the > inheritance implementation. > The Problem with this procedure of wrapping the class.create code in > an anonymous function, is that that > these private members are also private if the super method is called > from the inherited class. > > var BaseClass = (function() > { > var _myPrivateMember = 5; > > return Class.create( > { > initialize: function(id) > { > }, > > // add public methods here > myPublicMethod: function() > { > alert(_myPrivateMember); > } > }); > > })(); > > // inherit from BaseClass > var InheritedClass = (function() > { > return Class.create(BaseClass, > { > initialize: function() > { > }, > > // override, from base class > myPublicMethod: function($super) > { > // do some stuff > $super(); > // calls method from super class, but the superclass > // method does not know its privat member any more > } > }); > > })(); > > If I call myPublicMethod from the inherited class, it tries to call > the method of the baseclass. > Unfortunetaly the Method of the baseclass doesn''t know the > privatmember anymore. > > That is, why I was asking, if their is a best practice method for > private methods with inheritance in prototype. > > I would really appreciate if their was a solution for this :( > > cheers > Dean--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---