In Javascript, when is a new scope created? The 2 situations I know of are:
- with a new function (update on 2012/09, I think it needs to be a function invocation, not just a function definition)
- 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.
Yes, there is a third case where the scope chain is augmented (besides the
letmozilla-extension that Shog9 mentions), when acatchblock is evaluated: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 thecatchblock.But keep in mind that it only augments the current scope temporarily, to introduce the
catchIdentifier, any variable declared inside will be simply hoisted to the top of it enclosing function.