Clarification regarding "top level" arrow functions and "this"/"arguments"
allen at wirfs-brock.com
Wed Jan 21 17:28:31 PST 2015
On Jan 21, 2015, at 10:21 AM, Brendan Eich wrote:
> Felix Kling wrote:
>> [Section 14.2.17](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-arrow-function-definitions-runtime-semantics-evaluation) says
>>> Any reference to `arguments`, `super`, or `this` within an *ArrowFunction* are resolved to their bindings in the lexically enclosing function.
>> However, what if there is no enclosing function? Shouldn't this say "enclosing environment"?
Well, that's just an informative note and not normative language. The details are actually more complicated than what is summarized there. The fuller story, if that using 'super' in a top level arrow function produces an early error (see http://people.mozilla.org/~jorendorff/es6-draft.html#sec-scripts-static-semantics-early-errors and http://people.mozilla.org/~jorendorff/es6-draft.html#sec-module-semantics-static-semantics-early-errors ). 'this' and 'arguments' are just resolved via lexical lookup ('this' actually uses a special lookup mechanism) and may resolve to top level bindings.
Substituting "environment" for "environment" is not exactly the right thing either because the enclosing environment may be a block environment which can't define 'this' but can define 'arguments' (in non-strict code).
What that note was trying to draw attention to is that arrow functions do not define their own bindings for 'this', 'super', and 'arguments' and that those are typically bound by some enclosing function.
Writing such notes is kind of tricky. They are intended to be clarifying but are not intended to replace the need to read and understand the actual normative specification language. By their nature, notes are less complete then the actual normative language (it is actually a bug to fully replicate the normative spec. in such notes).
Thanks for pointing this out. I like to get this sort of bug report because my assumption is that if even one person finds some text unclear then many others will also have the problem. Please file bugs when you find material that you find misleading or difficult to understand.
I already have some ideas for making this note clearer and better emphasizing its key points.
> Good catch, probably best to file a bug at this point:
>> If yes, what would be the expected behavior of an arrow function that references `this` and `arguments`?
>> I assume in case of `this`, it would just resolve to the *thisBinding* of the environment (if there is one). Since there is no `arguments` binding (by default) I assume accessing `arguments` would throw a reference error.
>> Is my understanding correct?
Yes, it all falls out of the normative spec. language.
> I think so, but SpiderMonkey has a bug, so it's hard to test that implementation.
> es-discuss mailing list
> es-discuss at mozilla.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss