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

Current behavior is a major bug/discrepancy in the specification. In general, terrible precedents have become a core part of JS language design culture #26

Open
celmaun opened this issue Feb 1, 2025 · 1 comment

Comments

@celmaun
Copy link

celmaun commented Feb 1, 2025

While I'm not opposed to this particular proposal because it's useful to have syntactic sugar for this.constructor.staticMethod() (as class.staticMethod()) in instance methods. Isn't the current behavior in static methods a bug? Why is it that:

class Parent { #x = 123; getX() { return this.#x; } }
class Child extends Parent { } 
new Child().getX() // -> 123

but

class Parent { static #x = 123; static getX() { return this.#x; } }
class Child extends Parent { } 
Child.getX() // -> Uncaught TypeError: Cannot read private member #x from an object whose class did not declare it

This TypeError makes no sense to me. Why is there such a major discrepancy in static vs instance methods when subclassing? I could understand retaining problematic functionality in decades old JS syntax for BC and the need for coming up with stuff like strict-mode. But now even for relatively new functionality, it has become a part of culture in JS language design that major new syntax is proposed/added to the language with added complexity and years are spent on debates to retain unintuitive erroneous behavior instead of making a simple fix w/ some urgency. This direction is quite obviously problematic and unhealthy for the future evolution of JS in the long-term.

See also, the assignment-override-mistake, tc39/ecma262#1320 tc39/proposal-stabilize#4

Sorry for using direct language to make the point, I don't mean to offend anyone.

@ljharb
Copy link
Member

ljharb commented Feb 1, 2025

The difference is in static private fields, specifically, and it was an intentional part of the design to prevent very common subclassing bugs wrt constructors with shared objects, as I recall.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants