4

In JavaScript a global variable is also a property of the window object. What about local variables? Are they the property of any object?

For example:

var apple=3;
alert(apple);                   //  3
alert(window.apple);            //  same

thing();

function thing() {
    var banana=4;
    alert(banana);              //  4
    alert(thing.banana);        //  doesn’t work, of course
}

Is banana a property of any object

Manngo
  • 14,066
  • 10
  • 88
  • 110

3 Answers3

7

What about local variables? Are they the property of any object?

No. When execution enters a function, a new declarative environment record is created for storing the identifiers.

Unlike object environment records (used for creating the global and with environments) there is no user space object to which the variables are mapped.

See also What really is a declarative environment record and how does it differ from an activation object?

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Thanks for the answer and especially the link. I guess the point here is that functions do not normally have their own Object Environment Record, while the global window object does. – Manngo Sep 10 '15 at 01:04
  • *"while the global window object does"* Objects don't have environment records, environments have environments records. The global environment happens to have of a global environments record whose binding objects is `window` (which makes it the global object). – Felix Kling Sep 10 '15 at 01:13
2

But you can still store stuff inside function object. This way you can have something called static variable in languages like C/C++. For example:

function thing() {
    thing.banana = (thing.banana + 1) || 1;
    alert('This function was ran '+ thing.banana + ' times.');
}

thing(); // alerts "This function was ran 1 times"
thing(); // alerts "This function was ran 2 times"
thing(); // alerts "This function was ran 3 times"
thing(); // alerts "This function was ran 4 times"
thing(); // alerts "This function was ran 5 times"
alert(thing.banana) // alerts 5
Andrew Dunai
  • 3,061
  • 19
  • 27
  • I knew that, but thanks for making the point so that it’s part of this discussion. I takes advantage of the fact that a function is also an object. – Manngo Sep 10 '15 at 01:05
0

I pulled this from scope and modify so that you can learn a bit more tricks. Check this example out.

<!DOCTYPE html>
<html>
<body>

<p>
In HTML, all global variables will become window variables.
</p>

<p id="demo"></p>

<script>
var apple=3;
var obj=new thing();
document.getElementById("demo").innerHTML =
"I can display " + window.apple + " and " + window.banana + " but not local " + window.orange + ". I can call this from getter though " + obj.getOrange();


function thing() {
    banana=4;
    var orange=5;

    this.getOrange = function(){
        return orange;
    }

}
</script>

</body>
</html>

Output will look like this.

In HTML, all global variables will become window variables.

I can display 3 and 4 but not local undefined. I can call this from getter though 5

So unless you create the getter and setter for local variables, you will not be able to reference them. Global cariables will become window varaibles.

Pyrogrammer
  • 173
  • 1
  • 12