Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pros and Cons of function-level scope (Specifically in Javascript)

Tags:

javascript

What are the pros and cons of function-level scope specifically in Javascript compared to block-level scope in languages like Java?

I would like to see examples of function-level scope usage that would be harder or impossible to implement using block-level scope.

like image 746
Glide Avatar asked Sep 24 '10 22:09

Glide


People also ask

What is function level scope in JavaScript?

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 difference between function scope and block scope in JavaScript?

Function Scope: When a variable is declared inside a function, it is only accessible within that function and cannot be used outside that function. Block Scope: A variable when declared inside the if or switch conditions or inside for or while loops, are accessible within that particular condition or loop.

What is the difference between scope and function?

The scope determines the accessibility of variables and other resources in the code, like functions and objects. JavaScript function scopes can have two different types, the locale and the global scope. Local variables are declared within a function and can only be accessed within the function.

What are the types of JavaScript function scope?

There are three types of scope in JavaScript — 1) Global Scope, 2) Function Scope, and, 3) Block Scope.


2 Answers

The first example that comes to mind is: JavaScript's handling of closures would be much more expensive if implemented with block-level scope.

When you enter a function in JavaScript, an object is allocated (well, a couple, but we'll focus on one) that ends up being the "variable object" — that is, where all the arguments and local vars for that function call are held (as properties). It's this object that the closure actually uses (not just the "symbols" it appears to use; that's a common misconception). These objects are strung together in a chain, called the scope chain, which is used for resolving unqualified symbols.

Imagine how much more expensive that would be if every block introduced new scope.

like image 100
T.J. Crowder Avatar answered Oct 30 '22 23:10

T.J. Crowder


I would like to see examples of function-level scope usage that would be harder or impossible to implement using block-level scope.

Maybe it will sound obvious, but you can implement recursion in function-level scope, which can be often useful, for example:

var x = 5; // global scope

(function (y) { // y - locally scoped variable on each execution
  y && arguments.callee(--y); // recursion!
  console.log(y);
})(x);

That is mostly impossible to implement with block-level scope.

In the above example the function will initially be executed passing the value of the outer x variable to it, before the function is invoked a new execution context is setup, that initializes a new lexical scope, where the y formal parameter is bound to it.

After that, the function expression is executed again -if y is not 0- initializing on each execution a completely new lexical scope.

like image 38
Christian C. Salvadó Avatar answered Oct 30 '22 22:10

Christian C. Salvadó