1

If functions are objects in javascript, why can't I access the function scope defined variables?

I understand that in the code:

// variable test assigned an anonymous function
var test = function(){
    var x = 5;
};
console.log(test.x); // undefined

// Even when the function is explicitly named:
function test(){
    var x = 5;
}
console.log(test.x); // undefined

I don't need to get this working or anything; I just need to understand why functions are like this.

Thanks.

  • The variables you've created are visible in the scope of the function. And yes, they are objects, so you can do `var test = function() {}; test.x = 5;` – andrusieczko Jan 15 '15 at 05:08
  • 2
    Local variables are not magically becoming properties of the function object. And at no point does a variable `x` even exist in your application, because `test` is never executed. – Felix Kling Jan 15 '15 at 05:09
  • You probably meant `var test = function(){this.x=5; console.log(x);}; test.x = 3; test(); console.log(test.x);`; so here when you write `this.x=5` and then you call the function like `test()`, it is called with scope of `window`. So effectively, `this.x=5` in the body of the function is an equivalent to `window.x = 5`. Now, console.log(x) in this case tries to find the local variable `x` and then, the global variable `x`. So `console.log(x)` is here the same as `console.log(window.x)`. So you've got `test.x === 3` and `window.x === 5`. – andrusieczko Jan 15 '15 at 14:43
  • @andrusieczko If I make this code: var test = function(){x=5; console.log(x);}; test.x = 3; test(); console.log(test.x); The console will log 5, 3 respectively. I understand that scope prevents my test.x assignment from changing the function variable. So does that mean test is an object with property x = 5 as well as executable code with private variable x = 3? –  Jan 15 '15 at 14:45
  • if you mean `x=5`, then it's even more clear that you're reffering to a "global" context which is (in most cases) the `window` object. I hope it helps! – andrusieczko Jan 15 '15 at 14:47

6 Answers6

2

This would be one way to accomplish what you are trying:

function test() {
    this.x = 5;
}

var foo = new test();
console.log(foo.x);

Using var x rather than this.x just declares a local variable

asimes
  • 5,749
  • 5
  • 39
  • 76
0
var someName = function(){var x ...} /// only has local scope.

What is the scope of variables in JavaScript?

Will describe it better than I can. Good job on being curious and motivated.

Community
  • 1
  • 1
terary
  • 940
  • 13
  • 30
0

Functions are objects, but that doesn't mean that any variable declared inside the function becomes a property of that function object. In fact, that would be terrible because you wouldn't be able to run a function more than once (since the second time, the variables would start with different values).

You can assign a property to a function object like this:

var test = function () {};
test.x = 5
stubailo
  • 6,077
  • 1
  • 26
  • 32
  • Ah this really makes sense too. I mistakenly assumed the variable declarations in the function belonged to itself. I should have looked at it as statements that are created within the function scope when invoked. Thanks! –  Jan 15 '15 at 15:02
0

The variable is visible only in the function and it is possible to access it only within the function, you can use global variable and then edot it insode the function.

0

You have created Local variables. Local variable can only be accessed within the function.

Try to understand about Local & Global JavaScript Variables

Local JavaScript Variables

Variables declared within a JavaScript function, become LOCAL to the function.

Local variables have local scope: They can only be accessed within the function.

function myFunction() {

    var name = "roy";

    // code here can use name

}

Global JavaScript Variables

A variable declared outside a function, becomes GLOBAL.

A global variable has global scope: All scripts and functions on a web page can access it.

var name = "roy";

// code here can use name

function myFunction() {

    // code here can use name

}
shanidkv
  • 1,118
  • 1
  • 9
  • 12
0

I believe it is because those variables only exist within the scope of the function you have defined. Outside the scope of that function they do not exist.

They are the equivalent of private members in a class of an object oriented language.

Alternatively you could have

function test() {
  this.x = 5;
}

var testInstance = new test();

console.log(test.x);
Michael Dowd
  • 221
  • 1
  • 5
  • Yeah, I was wondering about why it wasn't clearly visible without assigning it to another variable. But I think you're right. I completely forgot about function scope. It makes complete sense now. –  Jan 15 '15 at 14:02