by lifthrasiir · 5y ago · 0 comments
I can agree that classes have been more or less a syntactic sugar, but private identifiers are not. The field name collision is a problem in virtually every dynamically typed language with namespace inheritance and each language has their solutions (see Python `__mangled_name` and Ruby `@instance_var`), JavaScript just happens to have another solution and that alone justifies a separate `class` syntax (since the scope of such private identifiers should be lexical).
Remember, once upon a time JS was supposed to be Scheme...
Your approach can't translate the following code:
class MyNumber {
#value;
constructor(value) { this.#value = value; }
add(other) { return new MyNumber(this.#value + other.#value); }
}
The proper solution is to use a non-enumerable property with an unnamed Symbol name, which is possible but cumbersome.