-
Notifications
You must be signed in to change notification settings - Fork 107
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
Other ways of declaring private fields #33
Comments
We did discuss making a (technically) redundant The working theory of the proposal is that users will learn that Many programmers have written in for bugs saying they'd prefer private fields declared with a |
Thanks for answering. It makes kind of sense and I'm also among the people who dislike the What I actually dislike about that char is that is similar to I was going to suggest the following before: class {
static prop = 0
private prop = 0
prop = 0
method() {
new.target.prop // refers to static field
new.self.prop // would refer to private field
this.prop // would refer to own
}
}
// Or
class {
#prop = 0
method() { new.#prop }
} However, that leaves private I do have one question. A private static field cannot be accessed from an instance method of the same class, right? class {
static #prop = 0
static method() {
this.#prop
}
method() {
new.target.#prop // < No?
new.privateFields.#prop // < Or something like this alternatively
}
} Since The point is, if a |
Just curiosity. Because many people asked about class A {
static private #a = 0
static protected #b = 0
private #a = 0
protected #b = 0
method() {
this.#a
this.#b
}
}
// Other class
class B extends A {
method() {
this.#b // ok
this.#a // throws
}
} |
The intention of this proposal is to leave protected as a follow-on provided by decorators. |
Thanks for filing this issue. Please reopen if you see a problem with the current plan of sticking with the |
I'm not sure if this is the best place to ask. Since I couldn't get a clear answer why other methods for declaring private fields where not considered in this thread tc39/proposal-class-fields#59
I would like to know if here somebody could clarify.
To sum up,
I read the FAQs and I get why the
#
chars was chosen. What I disagree with is the way private fields are declared.In the discussion I mentioned the following example to be confusing:
Here, there is nothing that makes the code self explanatory.
$
and_
are still perfectly valid chars to define a variable.What about:
I asked later in the thread, why isn't this syntax better?:
Why?
Public fields are defined without the
public
keywordStatic fields are defined with the
static
keywordPrivate fields are defined the same way public fields are with special chars
_
and$
which makes it confusing and the code less self explanatoryBy not using a keyword
private
, it may limit future proposals for other kinds of fields (protected
,friend
for instance. Would they need to come up with another unused character?)Could somebody clarify if these points where taken into account? If so, does that mean defining vars beginning with
_
and$
should be considered a bad practice in the future to avoid making the code not readable when working with private fields?The text was updated successfully, but these errors were encountered: