-
Notifications
You must be signed in to change notification settings - Fork 22.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document two JS errors with classes #32857
Conversation
Preview URLs
(comment last updated: 2024-04-29 16:08:23) |
files/en-us/web/javascript/reference/errors/cant_delete_private_fields/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/javascript/reference/errors/arguments_not_allowed/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/javascript/reference/errors/arguments_not_allowed/index.md
Outdated
Show resolved
Hide resolved
|
||
## What went wrong? | ||
|
||
A class field initializer expression or a class static initialization block cannot read from its scope the `arguments` identifier. Doing so is a syntax error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A class field initializer expression or a class static initialization block cannot read from its scope the `arguments` identifier. Doing so is a syntax error. | |
A class field initializer expression or a class static initialization block cannot read the `arguments` of the function that contains the class. Doing so is a syntax error. |
See below; this is just an example.
- This is true even if `arguments` is bound in a parent scope. | ||
- A non-arrow function declared in the previous conditions will still bind and be able to read its own `arguments` identifier. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nataliaschlebinger Maybe you know this already, but if it helps you put into context why this error is needed:
Class static blocks and field initializers are actually wrapped into functions. For example:
class A {
static {
// ...
}
}
// Becomes:
class A {
}
(function () {
// ...
}.call(A));
class A {
foo = this.y;
}
// Becomes:
class A {
constructor() {
this.foo = function () {
return this.y;
}.call(this);
}
}
This is why the language has to artificially impose this arguments
restriction here, because otherwise you would actually not be able to read arguments
of the parent, but the arguments
of this synthesized function.
Maybe it's useful to be put in the description? I'm not sure. But, I think the scoping effects as explained here are still confusing. The only thing to be aware of is that initializers/static blocks basically create their own arguments
scope boundary, except that in this scope arguments
is inaccessible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know this thank you and thanks for the detailed explanation! Still, I don't understand why the explanation for the scoping effects is confusing. Is it talking about "the scope of an expression"? By that I meant the scope that the expression can "see".
I don't wanna talk about mixing classes and functions, because I don't want people to think of the times they've heard that "classes are just syntactic sugar for constructor functions", I would like them to think of classes as a thing in their own right. (Maybe I'm wrong!!)
Do you think it's necessary to talk about this? I'm questioning because it's a thing the language does internally, and only the restriction is exposed to the programmers. If it did this internally but with no restriction, arguments
in our case would just always be empty, for example. If it is necessary, I'll write it in no problem!
Thank you for your time.
files/en-us/web/javascript/reference/errors/arguments_not_allowed/index.md
Outdated
Show resolved
Hide resolved
files/en-us/web/javascript/reference/errors/arguments_not_allowed/index.md
Outdated
Show resolved
Hide resolved
Sorry, am busy this weekend. I'll try to find some time next week to take another look. Looks great at a glance already |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. Thanks!
Description
Document the errors:
Motivation
Make the error reference richer and more linkable from the dev tools.
Additional details
Context can be found in this issue.