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:
- with new function (update on 2012/09, think needs function invocation, not function definition)
- 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:
let c parameter has been passed production.
create new object if expression new object().
create property in object result(2). property's name identifier, valueisc. value, , attributes { dontdelete }.
add result(2) front of scope chain.
evaluate block.
remove result(2) front of scope chain.
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
Post a Comment