By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,253 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,253 IT Pros & Developers. It's quick & easy.

Re: Replacing a private function an keeping access to private variables

P: n/a
Gregor Kofler wrote:
Expand|Select|Wrap|Line Numbers
  1. function foo() {
  2.    var privateVar = 42;
  3.    var privateFunc = function() {
  4.      alert(privateVar);
  5.    };
  6.    return {
  7.      replacePF: function(f) { privateFunc = f; },
  8.      accessPF: function() { privateFunc(); }
  9.    };
  10. }
  11. var obj = foo();
I am curious, why did you choose to use a factory instead of a constructor?
Expand|Select|Wrap|Line Numbers
  1. obj.accessPF(); // 42
  2. obj.replacePF(function() {
  3.    window.alert("The answer is "+privateVar);
  4. });
  5. obj.accessPF(); // privateVar is undefined
It's a closure and `privateVar' in privateFunc() is bound to the execution
context of foo(), in which `privateFunc' was declared a variable. However,
the function expression in the obj.replacePF() call creates a closure in
which `privateVar' is bound to the execution context in which the call is
executed and the anonymous Function object is created, and in which scope
chain no object has such a property.

ISTM the only way to do this is to have a public method that returns
`privateVar', which would become "protected" through this: Either

Expand|Select|Wrap|Line Numbers
  1. function foo()
  2. {
  3. var privateVar = 42;
  4.  
  5. var privateFunc = function() {
  6. window.alert(privateVar);
  7. };
  8.  
  9. return {
  10. getPrivateVar: function() { return privateVar; },
  11. replacePF: function(f) { privateFunc = f; },
  12. accessPF: function() { privateFunc(); }
  13. };
  14. }
  15.  
  16. var obj = foo();
  17.  
or

Expand|Select|Wrap|Line Numbers
  1. function Foo()
  2. {
  3. var privateVar = 42;
  4.  
  5. var privateFunc = function() {
  6. window.alert(privateVar);
  7. };
  8.  
  9. this.getPrivateVar = function() { return privateVar; };
  10. this.replacePF = function(f) { privateFunc = f; };
  11. this.accessPF = function() { privateFunc(); };
  12. }
  13.  
  14. var obj = new Foo();
  15.  
  16. and then
  17.  
  18. // 42
  19. obj.accessPF();
  20.  
  21. obj.replacePF(function() {
  22. window.alert("The answer is " + obj.getPrivateVar());
  23. });
  24.  
  25. // "The answer is 42"
  26. obj.accessPF();
(It's the usual simple getter and setter implementation as it is.)


HTH

PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Thomas 'PointedEars' Lahn meinte:
I am curious, why did you choose to use a factory instead of a constructor?
I've started switching from constructor instantiation to a factory like
pattern after reading Crockfords new book, and realizing that I tend to
have objects with a meager amount of public properties, but lots of
private functions. On the other hand I do need a factory occassionally.
Since I try to avoid mixing the patterns, I stick to factories most of
the time now.
It's a closure and `privateVar' in privateFunc() is bound to the execution
context of foo(), in which `privateFunc' was declared a variable. However,
the function expression in the obj.replacePF() call creates a closure in
which `privateVar' is bound to the execution context in which the call is
executed and the anonymous Function object is created, and in which scope
chain no object has such a property.
Yep, I know the reason, why I can't access it this way, but I was rather
tired yesterday evening and had this feeling that missed something.
ISTM the only way to do this is to have a public method that returns
`privateVar', which would become "protected" through this
[snip]

Ok, that's the approach I already use.
HTH
Sort of.

Gregor


--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.