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 new scope created? 2 situations know of are:

  1. with new function (update on 2012/09, think needs function invocation, not function definition)
  2. in "with" statement

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

is there third situation new scope created besides 2 situations above? thanks.

yes, there third case scope chain augmented (besides let mozilla-extension shog9 mentions), when catch block evaluated:

the production catch : catch (identifier ) block evaluated follows:

  1. let c parameter has been passed production.

  2. create new object if expression new object().

  3. create property in object result(2). property's name identifier, valueisc. value, , attributes { dontdelete }.

  4. add result(2) front of scope chain.

  5. evaluate block.

  6. remove result(2) front of scope chain.

  7. return result(5).

so basically, new object created, property named identifier passed catch, new object added scope chain, able use identifier within catch block.

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

but keep in mind augments current scope temporarily, introduce catch identifier, variable declared inside hoisted top of enclosing function.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -