Clarification regarding "top level" arrow functions and "this"/"arguments"

# Felix Kling (10 years ago)

Section 14.2.17 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"?

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?

# Brendan Eich (10 years ago)

Felix Kling wrote:

Section 14.2.17 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"?

Good catch, probably best to file a bug at this point:

bugs.ecmascript.org

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?

I think so, but SpiderMonkey has a bug, so it's hard to test that implementation.

bugzilla.mozilla.org/show_bug.cgi?id=889158

# Felix Kling (10 years ago)

On 2015-01-21 10:21, Brendan Eich wrote:

Good catch, probably best to file a bug at this point:

bugs.ecmascript.org

Filed as ecmascript#3587

# Allen Wirfs-Brock (10 years ago)

On Jan 21, 2015, at 10:21 AM, Brendan Eich wrote:

Felix Kling wrote:

Section 14.2.17 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 people.mozilla.org/~jorendorff/es6-draft.html#sec-scripts-static-semantics-early-errors and 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:

bugs.ecmascript.org

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.

# Gary Guo (10 years ago)

I think the spec is clear. The hasThisBinding of function environment record of arrow function will return false, thus always trace to the outer environment - in your case, could be global environment record or module environment record, which resolves the this binding to either global object or undefined.

# Brendan Eich (10 years ago)

ECMA-357 (E4X) pioneered informative-first prose sections, not found in ECMA-262 Ed. 3, and as a direct consequence, had too many imprecise or even inaccurate informative notes, which (turns out) were misread as normative, or simply otherwise caused confusion.

Thanks for looking into fixing this.

# Felix Kling (10 years ago)

Thank you everyone for your input and explanation, I really appreciate it!

I just wanted to say that I understand that those notes are not normative, however, I also think they are tremendously helpful for understanding more "complex" parts of the spec. I think they can be a gateway until one is more familiar with reading the spec.

In my particular case, the normative part was quite clear, however having the note in mind, I thought I might be overlooking something somewhere.

Thank you again for the clarification and the hard work that goes into creating this spec!