25

Say I have this anonymous function:

(function(window){

 var private = 'private msg';

 function sayit() {
   alert(private) // works
 }

 document.body.onclick = sayit; // works

})(window);

// private shouldn't be accessible here

Is this how JavaScript should behave?

That is, there is no way to access private from anywhere outside of that anonymous function?

If so, is it possible to find some kind of hack to access private from the outside, leaving the code the way it is?

steve
  • 3,878
  • 7
  • 34
  • 49

5 Answers5

14

Yes, this is how Javascript lets you have 'private' variables (hidden in a function scope).

No, there's no hack available to access variables such as private without re-writing the code.

Variables defined with var within a function can be accessed only from within that function.

Faiz
  • 16,025
  • 5
  • 48
  • 37
10

Ok. I got it.

(function(window){
    var alert_original = window.alert;
    window.alert = function(data) {
        window.extracted = data;
        alert_original(data);
    };
})(window);

(function(window){
    var private = 'private msg';
    function sayit() {
    alert(private) // works
 }
 document.body.onclick = sayit; // works
})(window);

After you click body, you can get 'private msg' from extracted

SangYeob Bono Yu
  • 821
  • 4
  • 16
  • 1
    Clever. Only, you can't *change* the value of `private` so what you have is read-only 'access'. – Faiz Jan 17 '13 at 07:14
2

They aren't intended as "private" variables; that's just how closures work. You can do the same thing in Perl and Python, at the very least, and probably a great many other languages with closures and lexical scoping.

Debuggers like Firebug or Chrome Inspector can still show you the entire stack at any point (including closed-over variables), but other than that and without changing the original code, I think you're out of luck.

Perhaps if you told us your actual problem... :)

Eevee
  • 47,412
  • 11
  • 95
  • 127
  • Does closure == anonymous function? – steve Jan 17 '13 at 07:30
  • 1
    @barjonah - they are related but not the same. It is easy to form a closure with the declaration of an anonymous functions. A closure is formed when an anonymous (or even a nested or regular named function) refers to variables in it's parent scope. So an anonymous function is one way of creating a closure. – Faiz Jan 17 '13 at 07:38
  • i can't help but feel i've [answered this before](http://stackoverflow.com/a/150185/17875) :) but Faiz is correct, yes. – Eevee Jan 17 '13 at 07:53
0

That's the whole point of having scope and private variables

Either

Set the private value to a global variable?

or

declare the variable outside

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
-3

You would have to do something like this:

var Test = (function(window){
 var private = 'private msg';
 var api = {};

 function sayit(){
   alert(private) // works
 }
 document.body.onclick = sayit; // works

api.sayit = sayit;
return api;
})(window);

Test.sayit(); //this would alert 'private msg'
petermk
  • 757
  • 1
  • 6
  • 18