joe t. wrote:
> i tried Allen's sample in Firebug, and someVar is accessible from
> outside.
> 
> aWorker.someVar = "outside value";
> console.info(aWorker.someVar); // "outside value"
> 
> Also works when a new instance of the class is assigned:
> 
> var x = new aWorker();
> x.someVar = "outside value";
> console.info(x.someVar); // "outside value"
Hi Joe,

by the "console.info (...)" lines you mean you typed that stuff in
Firebug's command line, correct?

Try executing
  x.publicMethod ();
you should get a different result.

x.someVar really is not the same variable as the someVar from the
creator function.  It *is* this.someVar seen from inside any of the
methods, that's why they use plain someVar (without this) instead.
someVar is a local variable in the scope of the creator function (the
methods defined there really are closures, accessing the scope of their
definition).

There is *no* access control in Javascript, and thus there are no
private variables (anything prefixed by "this." is just a property of
the object that happens to be called "this" at the time).

But using local variables and closures as in Allen's example one can get
really close: The user of objects created in this way simply has no way
of accessing the local variables of the function used to seed (in lieu
of using the term "prototype") the object.

These variables are private in that they aren't known to the outside
world, not in that they are protected in any way.

Have fun
----Daniel



> On Sep 10, 2:45 pm, Ngan <nganp...@gmail.com> wrote:
>> Hi Allen, thank you so much for the reply.
>>
>> If I do it that way, someVar will now be a private variable.  What if
>> I wanted someVar to be an instance variable.  so that it may be
>> accessed externally:
>>
>> aWorker.someVar
>>
>> hence why I did:
>>
>> this.someVar = 'var';
>>
>> Is this something that you have to give up when going with this
>> pattern?
>>
>> On Sep 10, 11:07 am, Allen <bla...@gmail.com> wrote:
>>
>>> Hi Ngan,
>>> This should do what you want.
>>> var Worker = Class.create((function() {
>>>  var someVar;
>>>  function initialize() {
>>>    someVar = "var";
>>>    privateMethod();
>>>  }
>>>  function publicMethod() {
>>>    privateMethod();
>>>  }
>>>  function privateMethod() {
>>>    console.info(someVar);
>>>    console.info(this)
>>>  }
>>>  return {
>>>   initialize: initialize,
>>>   publicMethod: publicMethod
>>>  };
>>> })());
>>> On Sep 10, 1:10 pm, Ngan <nganp...@gmail.com> wrote:
>>>> Hi, I apologize if this question has been asked before. I've tried
>>>> googling for this, but did find a good answer.
>>>> What's the best way to have private methods using prototype's
>>>> Class.create?
>>>> I've tried...
>>>> var Worker = Class.create((function() {
>>>>  function initialize() {
>>>>    this.someVar = "var";
>>>>    privateMethod();
>>>>  }
>>>>  function publicMethod() {
>>>>    privateMethod();
>>>>  }
>>>>  function privateMethod() {
>>>>    console.info(this.someVar);
>>>>    console.info(this)
>>>>  }
>>>>  return {
>>>>   initialize: initialize,
>>>>   publicMethod: publicMethod
>>>>  };
>>>> })());
>>>> Running this will give me:
>>>> aWorker = new Worker();
>>>> # console.info(this.someVar)  #=> undefined
>>>> # console.info(this.someVar)  #=> DOMWindow
>>>> However:
>>>> aWorker.publicMethod() #=> WORKS!
>>>> Thank you for your time!
>>
> > 


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to