Expected function parameter scoping behavioor
Allen Wirfs-Brock
allen at wirfs-brock.com
Tue May 9 20:19:41 UTC 2017
You misunderstand. The two scope design is all about preventing closures from referencing variables declared within the body of the functions. See https://github.com/tc39/tc39-notes/blob/master/es6/2013-09/default-arguments.pdf <https://github.com/tc39/tc39-notes/blob/master/es6/2013-09/default-arguments.pdf> for the motivation for this design. (but be aware that there are probably some subtle differences between the final specified design and what this deck describes.
In you to test cases, `arg` is not declared within the body of `fn` so there is no duplicate binding for it. Things would be different if you had a `var` or `function` declaration for `arg` within the body.
Allen
> On May 9, 2017, at 12:40 PM, Logan Smyth <loganfsmyth at gmail.com <mailto:loganfsmyth at gmail.com>> wrote:
>
> Hey all,
> We're currently exploring some changes to Babel's behavior for function parameter default scoping, and I was hoping to validate my understanding of the spec, because my reading of the spec does not conform to Chrome or FF's behavior. Alternatively if you know bugs for those engines for this, let me know.
>
> My understanding from https://tc39.github.io/ecma262/#sec-functiondeclarationinstantiation <https://tc39.github.io/ecma262/#sec-functiondeclarationinstantiation> step #27 is that if function parameters contain any expressions, the function body is shifted to run in a declarative environment separately from the params. Per #27.f.i.4.a, the initial values of the params are copied from from the param environment into the function body environment, at initial execution time.
>
> Given that, I'd expect cases such as
> ```
> (function fn(arg, setValue = function () { arg = "a string" }) {
> setValue();
>
> return arg;
> })("initial")
> ```
> to return `initial`, because the `arg` binding being mutated by `setValue` is not tied to the `arg` binding that the function returns, as the `"initial"` value would have been copied into the function body binding before `setValue` ran.
>
> Is my understanding there correct?
>
> Similarly I'd have expected
> ```
> (function fn(arg, getValue = function () { return arg; }) {
>
> arg = "new value";
>
> return getValue();
> })("initial")
> ```
>
> to return `"initial"` because it is returning the binding value from the declarative environment in the function params and accessed by `getValue` is not the same environment being mutated by the assignment to `arg1`.
>
> Is my understanding correct, or is there something I'm misunderstanding?
>
> Thanks!
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170509/d3de7646/attachment.html>
More information about the es-discuss
mailing list