Thanks for the quick reply, Ryan That makes things much clearer.
On Sep 7, 5:41 pm, "Ryan Gahl" <[EMAIL PROTECTED]> wrote: > There are two good ways to do this. The first is what you already > mentioned... place the methods in the constructor, which as you found out > also requires that you place your public methods within the constructor. You > will find there are two camps on either side of that model. I for one use > that very model for many of my classes. For classes that will be > instantiated thousands of times in a session (and won't be properly > disposed/cleaned up), this is arguably not the most efficient way (memory > wise). Kangax will attest to that :) > > that looks like this: > > var myClass = Class.create({ > initialize: function() { > function myPrivateMethod() { > //do something... > > } > > //public "privileged" methods: > this.myPublicPrivilegedMethod = function() { > //can access private methods > myPrivateMethod(); > > }; > } > }); > > For more on this concept of "privileged" > members:http://javascript.crockford.com/private.html > > The other way is simple encapsulation. You create an anonymous, self calling > function scope, define the private methods before defining the class, and > make the private methods accept an instance of the class (essentially making > the private methods static, but only visible to the static or instance > methods of the class defined within the same scope. > > NOTE: In order to make the defined class visible outside the encapsulation > function you should define a namespace object at least one scope level up. > > //some scope level higher than the encapsulation function: > var myNamespace = {}; > > //create the anonymous function scope (closure) for encapsulation > (function() { > > //private methods (static so if they need to act on an instance, make them > accept one in the arguments) > function myPrivateStaticMethod(someInstance) { > //do something with the instance... > > } > > myNamespace.myClass = Class.create({ > initialize: function() { > //constructor logic as normal > > //can access private methods here... > myPrivateStaticMethod(this);}, > > somePublicPrototypeMethod: function() { > //...or here > myPrivateStaticMethod(this); > > } > }); > })(); //self calling > > Note there still are some major differences in what you can do with the two > models. For example, the first model (the one you already started using, > allows access to private instance variables that you define in the > constructor (both from within your private methods and public "privileged" > methods). The 2nd model saves a bit on memory if this is a class you'll > instantiate thousands of times, but you don't get the same ability to have > truly private instance members. > > We use both models for different scenarios, and often both of them for the > same class for different reasons. > > Javascript is beautiful :) > > > > On Sun, Sep 7, 2008 at 10:58 AM, webbear1000 <[EMAIL PROTECTED]> wrote: > > > Hello peeps! > > > I'm fairly new to Prototype and just about getting the hang of things > > so please be gentle with me :-) > > > I've got this sticky little problem that's giving me grief. I want a > > function to be private and accessible to a couple of a classes > > methods. > > > I've tried making it private by placing it in initialize but of course > > it's only accessible within the initialize function itself. > > > Here's a bit of pseudo code to demonstrate > > > var Bear = Class.create({ > > initialize: function(){ > > var privateFunction = function(args) { code } > > }, > > method: function(){ > > var something = privateFunction(args) <-- PROBLEM! > > } > > }); > > > Any help greatly appreciated. > > -- > Ryan Gahl > Manager, Senior Software Engineer > Nth Penguin, LLChttp://www.nthpenguin.com > -- > WebWidgetry.com / MashupStudio.com > Future Home of the World's First Complete Web Platform > -- > Inquire: 1-920-574-2218 > Blog:http://www.someElement.com > LinkedIn Profile:http://www.linkedin.com/in/ryangahl --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---