Declaring multiple variable expressions on a single line is common practice and also a great shorthand syntax.
However, while reviewing several developer’s work recently, I noticed many are not aware this creates implicit global variables.
Can you spot the problem? Try the following:
Multiple variable chaining on the same line creates a leak to the global namespace. Many developers are surprised that local bar and baz inside helloworld() has overwritten global bar and baz.
Why does this happen?
This is because of how operator associativity works in JavaScript, or more simply how operators with the same precedence are evaluated. The interpreter evaluates the = operator as right-to-left associativity.
Therefore the line:
Is easier understood as:
This is evaluated as baz = 'local', baz has not been declared locally so check the Scope Chain we find baz was declared globally.
The result of this expression is local, which is returned to the next operator bar = 'local'. bar is not declared locally, so look to the Scope Chain or assign it to the global namespace and return the value local.
Finally, var foo = 'local' sees there is no operator preceding foo but the keyword var, so creates a local variable named foo and assigns it the value local.
The Solution
To avoid the global leak, we simply need to separate the variable declarations from assignments:
Now this is better, the local variables are contained in their scope and are completely hidden from the global namespace.
Summary
I actually think this pattern looks quite ugly, having to separate declarations from assignments, but since it stops implicit globals then I’m all for it.
Hopefully after this article I won’t be stumbling across quite as many global namespace leaks in the future!
原文链接:Chaining Variable Assignments in JavaScript: Words of Caution