Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript function scope

Tags:

javascript

Who can explain why result are [20, 20, 10, 10] in this code:

var x = 10;
var foo = {
  x: 20,
  bar: function () {
    var x = 30;
    return this.x;
  }
};

console.log(
  foo.bar(),
  (foo.bar)(),
  (foo.bar = foo.bar)(),
  (foo.bar, foo.bar)()
);

Links to specification are welcomed

like image 919
anton_byrna Avatar asked Feb 19 '10 09:02

anton_byrna


People also ask

What is the JavaScript function scope?

JavaScript has function scope: Each function creates a new scope. Variables defined inside a function are not accessible (visible) from outside the function. Variables declared with var , let and const are quite similar when declared inside a function. They all have Function Scope: function myFunction() {

What is the scope of a function?

A scope is a region of the program and broadly speaking there are three places, where variables can be declared: Inside a function or a block which is called local variables, In the definition of function parameters which is called formal parameters. Outside of all functions which is called global variables.

Which JavaScript has functional scope?

var is called as function scope that is if a variable is declared using var keyword it will be accessible throughout the function. let & const are also called as block scope that is they are accessible within that particular block.

Do functions have scope?

Global Scope and Local ScopeVariables defined inside a function are in the local scope . They have a different scope for every call of that function. This means that variables having the same name can be used in different functions.


1 Answers

Can't point you at specification, but i can highly recommend reading Douglas Crockford's "Javascript: The good parts". This book will help you understand most of the weird but great features of JavaScript.

As of your question:

  1. foo.bar(), this keyword in bar function is bound to foo object
  2. (foo.bar)() is the same as above,
  3. In javascript you can assign variables from right to left multiple times

    z = 3; x = (y = z); console.log(x); //3

functions are variables as anything else. So you are assigning the function foo.bar to foo.bar, but the parenthesis causes the assigned function to be returned, and then executed.

(foo.bar = foo.bar)(); 
//is the same as
var f = (foo.bar = foo.bar);
f(); 
//and this also the same as:
var f= foo.bar;
f();

The function returned from parenthesis is not bound to anything, so this will refer to global object, in case of browsers - to the window object.

4.. The clause (foo.bar, foo.bar)() is just alike:

a = (3, 4); //last value is returned, first just parsed.
//a contains 4

var f = (foo.bar, foo.bar); 
//f contains body of foo.bar function, 
f() // is executed  in the context of `global` object, eg. `window`. 

Please read about binding of functions in JavaScript.

like image 103
SWilk Avatar answered Oct 13 '22 15:10

SWilk