0

I know that variables are properties of other objects. For example:

var myVar = 'something';

is a property of the window object (if it is in the global scope of course).

if I want to find the variable's object, I just use the this variable. But:

function f() {
    var myVar2 = 'something';
}

Which object does myVar2 belongs to? (myVar belongs to window object, but what about myVar2?)

I would like to know that, thanks.

Idan Yadgar
  • 974
  • 1
  • 6
  • 16
  • 3
    "I know that variables are properties of other objects" - this is incorrect. – zzzzBov Aug 02 '12 at 01:16
  • 1
    Excellent answer: http://stackoverflow.com/a/101089/1048572, and you might be interested in [Getting All Variables In Scope](http://stackoverflow.com/q/2051678/1048572) – Bergi Aug 02 '12 at 01:29

3 Answers3

2

It doesn't belong to an object. It belongs to the scope of the function f. You access it by doing myVar within f. You cannot access it outside of f.

If you did

function f() {
  this.myVar = 1;
}

now you can do

var myF = new f();
myF.myVar

indeed, this how user defined objects are sometimes defined.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • But according to this: http://11heavens.com/everything-is-a-property-of-an-object-in-javascript everything is a property of an object. – Idan Yadgar Aug 02 '12 at 01:17
  • 1
    according to some people {} is called a json object, too. – hvgotcodes Aug 02 '12 at 01:22
  • 1
    You should've linked that site in your question. And no, the author of that article is just wrong. – Bergi Aug 02 '12 at 01:23
  • yes in his comments some are fighting back, with an example just like this, in fact... – hvgotcodes Aug 02 '12 at 01:24
  • perhaps you should mention something regarding his incorrect claim that the first example binds to the window object. – Josh Smeaton Aug 02 '12 at 01:27
  • 2
    Shall we compromise: given the way JS closures work you could describe local function variables as being like properties of the closure created for each invocation of the function, but this "object" is not an object you can access from your code. – nnnnnn Aug 02 '12 at 01:27
0

myVar2 belongs to the local scope (of f), and myVar the global scope.

Kurt
  • 7,102
  • 2
  • 19
  • 16
0

var does some interesting things. var statements are hoisted to the top of their functional scope. The var's functional scope is whatever function it happens to be in.

JavaScript doesn't have block-level scope, which means that:

(function () { //a closure to create new scope
    var foo;
    foo = 1;
    if (condition) {
        var bar;
        bar = 3;
    }
}());

...is equivalent to...

(function () {
    var foo,
        bar;
    foo = 1;
    if (condition) {
        bar = 3;
    }
}());

If the var statement has no parent, it will instead add the variable as a property to the global context, which in web browsers happens to be window.

This is the only time that using var will create a property. If want to create a property of an object, you simply have to set it:

(function () {
    var foo;
    foo = {};
    foo.bar = 'baz'; //this creates the `bar` property on `foo`
}());

JavaScript is a prototypal language with prototypal inheritance. Functions are first-class objects (because JavaScript isn't racist against functions). This means that functions can be used just like any other object.

You can set them:

(function () {
    var foo;
    //foo is now a function
    foo = function () {
        alert('Hello World');
    };
}());

You can set properties on them:

(function () {
    var foo;
    foo = function () {
        alert('Hello World');
    };
    foo.bar = 'baz'; //this works just fine
}());

You can even pass them as parameters:

(function () {
    var foo,
        bar;
    foo = function () {
        alert('Hello World');
    };
    bar = function (c) {
        c();
    };
    bar(foo); //guess what this does?
}());

Another cool thing that functions do, is they act as constructors. All functions are inherently constructors, you just need to call them using the new keyword:

(function () {
    var foo; //case sensitive
    //it doesn't matter whether you use `function Foo`
    //or `var Foo = function...`
    function Foo() {
        alert('Hello World');
    }
    foo = new Foo();
    foo.bar = 'baz';
}());

The important detail in using constructors is that the function's context (this) will be set to the object created by the constructor. This means that you can set properties on the object within the constructor:

(function () {
    var foo;
    function Foo() {
        this.bar = 'baz';
    }
    foo = new Foo();
    alert(foo.bar); //'baz'
}());
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 1
    I think the OP wants to know about [scope variable objects](http://es5.github.com/#x10), not hoisting and first-class-functions :-| – Bergi Aug 02 '12 at 01:37