Skip to content
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

Merged
merged 11 commits into from
Apr 29, 2024

Conversation

nataliaschlebinger
Copy link
Contributor

Description

Document the errors:

  • SyntaxError: private fields can't be deleted
  • SyntaxError: arguments is not valid in fields

Motivation

Make the error reference richer and more linkable from the dev tools.

Additional details

Context can be found in this issue.

@nataliaschlebinger nataliaschlebinger requested a review from a team as a code owner March 27, 2024 06:13
@nataliaschlebinger nataliaschlebinger requested review from Josh-Cena and removed request for a team March 27, 2024 06:13
@github-actions github-actions bot added Content:JS JavaScript docs size/m [PR only] 51-500 LoC changed labels Mar 27, 2024
Copy link
Contributor

github-actions bot commented Mar 27, 2024


## 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.
Copy link
Member

@Josh-Cena Josh-Cena Mar 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Comment on lines 37 to 38
- 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.
Copy link
Member

@Josh-Cena Josh-Cena Mar 30, 2024

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.

Copy link
Contributor Author

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.

@Josh-Cena
Copy link
Member

Sorry, am busy this weekend. I'll try to find some time next week to take another look. Looks great at a glance already

Copy link
Member

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thanks!

@Josh-Cena Josh-Cena merged commit 2ae0fc1 into mdn:main Apr 29, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:JS JavaScript docs size/m [PR only] 51-500 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants