Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Javascript, when is a new scope created? (with a new function and in a "with" statement) Are these the only 2 situations?

In Javascript, when is a new scope created? The 2 situations I know of are:

  1. with a new function (update on 2012/09, I think it needs to be a function invocation, not just a function definition)
  2. in a "with" statement

as a note, any new block (in if-then-else, loops, or just beginning a block for no other reason) won't create a new scope.

Is there a third situation where a new scope is created besides the two situations above? Thanks.

like image 659
nonopolarity Avatar asked Apr 28 '10 15:04

nonopolarity


People also ask

What are the two different scopes in JavaScript?

JavaScript has two scopes: global and local. Local scope has two variations: the old function scope, and the new block scope introduced with ES6. It's worth noting that function scope is actually a special type of a block scope.

How is scope created in JavaScript?

In JavaScript, scopes are created by code blocks, functions, modules. While const and let variables are scoped by code blocks, functions or modules, var variables are scoped only by functions or modules. Scopes can be nested. Inside an inner scope you can access the variables of an outer scope.

Which of these JavaScript keywords create a new scope?

With the introduction of let and const keywords, it added a new type of Scope in JavaScript. You cannot access the variables declared inside a particular block (represented by {}) from outside the block.

What are the three types of scope in JavaScript?

JavaScript has 3 types of scope: Block scope. Function scope. Global scope.


2 Answers

Yes, there is a third case where the scope chain is augmented (besides the let mozilla-extension that Shog9 mentions), when a catch block is evaluated:

The production Catch : catch (Identifier ) Block is evaluated as follows:

  1. Let C be the parameter that has been passed to this production.

  2. Create a new object as if by the expression new Object().

  3. Create a property in the object Result(2). The property's name is Identifier, valueisC. value, and attributes are { DontDelete }.

  4. Add Result(2) to the front of the scope chain.

  5. Evaluate Block.

  6. Remove Result(2) from the front of the scope chain.

  7. Return Result(5).

So basically, a new object is created, with a property named like the Identifier passed to catch, this new object is added to the scope chain, so we are able to use that identifier within the catch block.

try {
  throw "error";
} catch (identifier) {
  // `identifier` accessible here..
}

But keep in mind that it only augments the current scope temporarily, to introduce the catch Identifier, any variable declared inside will be simply hoisted to the top of it enclosing function.

like image 58
Christian C. Salvadó Avatar answered Nov 02 '22 00:11

Christian C. Salvadó


There's also the let statement. Keep in mind that let (...) {}, like with (...){}, does not create a new scope for variables introduced within the block. However, let definitions can create variables scoped to the block (any block) they're defined in.

Fair warning: as has been pointed out in comments, while let is part of JavaScript 1.7 (the Mozilla dialect of ECMA-262/ECMAScript), it is not part of ECMAScript, and will likely not work any time soon in browsers other than Firefox. Also note that while with can be used as a stand-in for let statements in current implementations of ECMAScript, the "strict" mode proposed for the pending ECMA-262 5th edition disallows this as well. If you're concerned about writing cross-plaform, future-proof code (and you should be...) then stick with functions for scope control!

like image 31
Shog9 Avatar answered Nov 02 '22 00:11

Shog9