Last active
November 28, 2017 22:45
-
-
Save ajklein/b947351835cc77ad0040db9a55813f51 to your computer and use it in GitHub Desktop.
Parameter scoping and sloppy eval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// What does this code do? | |
function f(x = eval("var z = 2; 1"), y = z) { | |
return [x, y, z]; | |
} | |
f(); | |
// Spec says to throw a ReferenceError, since 'z' is not visible either | |
// to other parameters in the list, or to the function body. | |
// | |
// Results in various engines: | |
// | |
// V8, SpiderMonkey, recent Chakra: ReferenceError: z is not defined | |
// Chakra (until recently): (early) SyntaxError: 'eval' is not allowed in the default initializer | |
// JavaScriptCore: returns [1, 2, 2] | |
// What about this? | |
function g(x = eval("var z = 2; 1"), y = z) { | |
var z; | |
return [x, y, z]; | |
} | |
g(); | |
// Spec, SM, V8: ReferenceError: z is not defined | |
// JSC: returns [1, 2, undefined] | |
// Proposal: switch the spec to the JavaScriptCore behavior. |
@ajklein I still prefer all eval being strict, thus no ability to make new var bindings at all in parameter expressions. I think that aligns closer with the intent of the current semantics. Is there a V8 implementation preference against that? Seems simplest of all possible semantics to me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@syg JSC apparently creates the var binding in the parameter scope, as adding a "var z;" to the function body causes the return value to be
[1, 2, undefined]
. I think that's the semantics I'd want.