-
Notifications
You must be signed in to change notification settings - Fork 3
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
Why is the # needed? #7
Comments
See this tc39/proposal-private-fields#14 for an explanation of the issues. |
That makes sense. I still think that omitting the Perhaps |
I'm not so sure about having the class Derived3 extends Base {
// adds an additional private slot that hides inherited slot2
private #slot2;
getData1() {
// returns undefined since subclass slot2 was not initialized
return #slot2;
}
} could just be class Derived3 extends Base {
// adds an additional private slot that hides inherited slot2
private slot2;
getData1() {
// returns undefined since subclass slot2 was not initialized
return this.slot2;
}
} On the other hand, I do think it might be nice to replace the accessor operand ( class Derived3 extends Base {
// adds an additional private slot that hides inherited slot2
private slot2; // <-- clearly private
getData1() {
// returns undefined since subclass slot2 was not initialized
return this#slot2; // <-- "#" instead of "." which denotes private access.
}
} so that when reading code it is clear that it's a private access, but the name of the variable isn't thought to include the |
And, what about for protected members? Maybe we can use a different symbol for that? Here are some with symbols that currently make SyntaxErrors: Or, maybe we can use one or two of the same symbol to denote protected and private, respectively: this#foo // protected
this##foo // private
this@foo // protected
this@@foo // private
this!foo // protected
this!!foo // private
this~foo // protected
this~~foo // private We could also combine symbols: this#foo // protected
this#>foo // private But, I think my favorite is one of these: this#foo // protected
this@foo // private
this#foo // protected
this~foo // private
this#foo // protected
this!foo // private The exclamation kind of helps the notion of private, as in "watch out, this is sensitive data!". None-the-less, I could live with a single symbol ( |
For private methods, the same could apply: class Foo {
private foo
protected bar
constructor() {
this!foo = 'foo'
this#bar = 'bar'
this#doSomething()
this!doSomethingElse()
}
protected doSomething() { /* ... */ }
private doSomethingElse() { /* ... */ }
} |
I forgot one symbol, the colon ( this.foo
this:foo // protected
this!foo // private! The only thing about the colon is that it might conflict with labels. But who uses those? Plus, it's easy to differentiate between a label for a loop and class Foo {
private foo
protected bar
constructor() {
this!foo = 'foo'
this:bar = 'bar'
this:doSomething()
this!doSomethingElse()
}
protected doSomething() { /* ... */ }
private doSomethingElse(otherFoo) {
console.log(otherFoo:bar)
console.log(otherFoo!foo)
}
} |
I may have missed why one of the above symbols can't be used. If so, just ignore that one. |
Oh, note that |
Other possibilities: this.foo
this.:foo // protected
this.!foo // private!
// or
this.foo
this.#foo // protected
this.!foo // private! while the declaration is still just private foo
protected foo without extra symbols (since the |
I think, after all this, I kind of like that last idea most, the augmentation of |
@zenparsing But, I do like the idea of There is this package |
@zenparsing A
|
``@` is not looked so good if es7 decorators (or any another annotation-liked syntax) will be also shipped in language: class Privateer {
@decotator private flag = 'GB';
constructor (flag) {
this.@flag = flag;
}
getCurrentAllegiance () {
return this.@flag;
}
} two different functionalities but with similar characters in their syntax is not looked so good. maybe something C++ like class Privateer {
@decotator private flag = 'GB';
constructor (flag) {
this->flag = flag;
}
getCurrentAllegiance () {
return this->flag;
}
} |
I thought of |
I think the only one I really didn't like was a plain |
Adding a random character to mark something private is unneccessary syntax. The compiler can treat it as such as well as any decent IDE. I suggest marking it as
This is similar to the compiled languages |
// What does this do?
Derived3.prototype.getData1.call({ slot2: "not-private" }); Without type information which can tell us whether a property name is private or public, we need a syntactically unambiguous way to make that determination. |
I would like to say that should be a compiler error, since using call in that way violates everything OOP about classes...but your right, its still supported. |
This proposal doesn't feel like it fits in with the language; the rules for access feel arbitrary and confusing. In addition, the I'd prefer that class Foo {
// The bar identifier is only accessible within this ClassBody,
// but is unique to each instance of the class.
private bar;
constructor(val) {
bar = val;
}
}
bar; // ReferenceError
Foo.prototype.baz = function() {
bar; // ReferenceError
}; I feel that this is a more natural fit for the language, and the rules around it are very simple to understand. If it is backed by private slots, that is fine by me, but that seems like an implementation detail (and therefore not worth introducing Additionally, I'm not sure how Also, it'd be great if private methods could be defined with the same semantics: class Foo {
private doSomeComplexTask(input) { /* ... */ }
constructor(input) {
doSomeComplexTask(input);
}
} (I actually find myself wanting private methods a lot more than private state, personally.) |
Omitting the class Foo {
private doSomeComplexTask(input) { /* ... */ }
constructor(input) {
this.-doSomeComplexTask(input);
}
} where the On Mon, Apr 18, 2016 at 10:15 PM, Stephen Scott
|
The |
What are the problems preventing the following syntax, and requiring the introduction of #?
The text was updated successfully, but these errors were encountered: