159 points · by stablemap · 7y ago · 134 comments
For those who don't know private class fields add the weird "#" character for defining them inside the `class {}` block.
class IncreasingCounter {
#count = 0;
get value() {
console.log('Getting the current value!');
return this.#count;
}
increment() {
this.#count++;
}
}
This looks way worse than having what TypeScript did by introducing `public` / `private` keywords. Is there any body
which votes for es-next features such as this one? I wonder how the "#" private field popped up in Chrome specs.1 : https://developers.google.com/web/updates/2018/12/class-fiel...
2 : https://developers.google.com/web/updates/2018/12/class-fiel...
https://github.com/tc39/proposal-class-fields/issues
There's at least three specifically about the #:
https://github.com/tc39/proposal-class-fields/issues/177
https://github.com/tc39/proposal-class-fields/issues/149
https://github.com/tc39/proposal-class-fields/issues/142
Remember to be constructive or just give a thumbs up to comments you like.
This is not to say that there aren't other viable solutions which would avoid the use of the current proposed syntax, but how do you weigh the advantages of avoiding a subjectively "ugly" syntax against the many other, much more concrete concerns that the TC39 has to evaluate when choosing a proposal? Should "the reflexive reaction of many devs when they first see the feature (without taking any time to try it out) is that the syntax is ugly" just be an automatic veto? If not, how strongly should that concern be weighted? Should an otherwise inferior proposal be accepted instead merely because the syntax is slightly prettier?
Ultimately, I think you need to have a person or group of people in authority who will look at all available options and make an informed decision, even over the objections of the uninformed majority. Right now, the TC39 _is_ that informed authority.
IMHO "No solution" would be better than "bad solution" in this case.
1 : https://github.com/tc39/proposal-class-fields/issues/100
class Example {
#myVar = 100;
getPrivateMyVar() {
return this.#myVar;
}
getLocalMyVar() {
return this.myVar();
}
}
const example = new Example();
example.myVar = 200;
console.log(example.getPrivateMyVar()) // 100;
console.log(example.getLocalMyVar()) // 200;
I agree the syntax is terrible. But you need to be able to distinguish between the own object property myVar and the private field myVar.example.myVar = 500 should throw an access error.
You don't need to distinguish because you shouldn't be shadowing private variables.
That removes a major benefit of private fields (which is to be able to change implementation details without breaking the public interface)
I don't think anyone really loves the syntax, but there doesn't seem to be much of a better way of doing it.
"Having a private field named x must not prevent there from being a public field named x, so accessing a private field can't just be a normal lookup. This is only an issue in JavaScript because of its lack of static types. Statically typed languages use type declarations to distinguish the external-public/internal-private cases without the need of a sigil. But a dynamically typed language doesn't have enough static information to differentiate those cases." [0]
There are also performance concerns to changing the lookup logic without the sigil. More info in the link.
0: https://github.com/tc39/proposal-class-fields/blob/master/PR...
> Having a private field named x must not prevent there from being a public field named x, so accessing a private field can't just be a normal lookup.
Why do classes support public/private fields of the same name in the first place? This seems fraught by nature?
class A {
#x = 100;
}
const a = new A();
a.x = 200;
On top of this, you should be able to define private variables and methods in derived classes without interfering with the base class private properties: class A {
#x = 100;
getA() { return this.#x }
}
class B extends A {
#x = 200;
getB() { return this.#x }
}
const b = new B();
b.getA() // 100
b.getB() // 200;I think the feature is good but couldn't they find a different character than one used for comments almost everywhere else?
But it is an object property, just like everything else attached to "this".
That "this.foo" is not "this.#foo" that's because "foo" is a different key than "#foo".
The current idea is that private fields are kinda like this:
export class Bar {
#foo = 100;
setFoo(value) {
this.#foo = value;
}
getFoo() {
return this.#foo;
}
getFooFromOtherBar(otherBar) {
return otherBar.#foo;
}
}
// similar to (but implemented differently I'm sure)
const barPrivate = new WeakMap(); // properties are only private if this is never exported
export class BarWithoutPrivateFields {
constructor() {
barPrivate.set(this, {});
barPrivate.get(this).foo = 100;
}
setFoo(value) {
barPrivate.get(this).foo = value;
}
getFoo() {
return barPrivate.get(this).foo;
}
getFooFromOtherBar(otherBar) {
return barPrivate.get(otherBar).foo;
}
}
It is NOT a property key!There has been a lot of discussion regarding public and private keywords here: https://github.com/tc39/proposal-class-fields/blob/master/PR...
JavaScript is not static, you don't have a compiler that can protect against access, so you need info that is preserved at runtime.
`#count` in this case is a different name than `count` and via this naming convention it's able to protect access. I don't get the point though, since people already use underscores as a soft mechanism for protecting access to private stuff and in a dynamic language like JS I don't think you need more.
I also think the TC39 proposals are doing more harm then good and they should just stop changing JavaScript. Just leave it the way it is, thanks.
"Promise" for example sucks, in spite of receiving early feedback, because TC39 is run by certain individuals that outright reject feedback that doesn't comply to their world view.
Please roll this back, what a nightmare!
Many system programmers will think this line is commented out. Indeed what a nightmare!
The muscle memory is so strong, when writing Javascript/Typescript I type # by accident sometimes.
But then, // is one of the most commonly used operators in another language I use, so it's weirdly symmetrical :)
Looks like Typescript conventions say not to do that. Either way the compiler should throw an error if trying to access a private incorrectly.
In JS I guess it helps reduce runtime errors since there is no compiler to check for you and again very clearly visible in code its a private.
Its sort of old school, like Hungarian notation where you would prefix variable with type identifiers except they really mean something.
I cringe every time I have to deal with such codebases.
Language design is hard but is there legitimate criticism besides subjective "I don't like this" ...
I wouldn't be surprised if this here is them giving that dominance a first shot. I'm sure they realize that developers will think it's horrible, but at the same time it's also not a problem. Nothing will break because of this, no company will be disadvantaged by it (barring Mozilla ofc). Perfect opportunity for testing the waters of a newly obtained monopoly position.
"V8 release v8"
At least when it's spelled out like this, they always use capital V8 for the engine and a lowercase v8 for the version.
I can imagine the verbal V8/v8 confusion probably forces everyone to say "version 8"
maybe
https://shop.gandi.net/en/domain/suggest?search=hackernews.d...
It makes it much more difficult to maintain downstream builds when the build-system breaks completely for indeterminate amounts of time.
You can check how the locales work here: https://github.com/unicode-cldr/cldr-misc-full/tree/master/m...
Go to the locale folder and then look at the listPatterns.json file
As noted by narrowtux, not all English locales use the Oxford comma; en-AU also doesn’t, which mollifies me.
The simplest style of example I can think of is nesting conjunctions: “the difference between A and B, and C and D”. (This example isn’t great because it depends on nesting and has only two items in each list; but it’s the first that springs to my mind. There are others that don’t require nesting or only two items, though normally not quite so clear-cut.) Removing the comma or shuffling things around in any way would completely change the meaning of the sentence. This is the sort of subtle thing that some will miss when writing, but in speech it’d be very clear.
https://v8.dev/blog/improved-code-caching
using this:
https://www.npmjs.com/package/v8-compile-cache
Or see the options with "cache" in their name, here: https://nodejs.org/api/vm.html#vm_new_vm_script_code_options
I thought they have like 10.000 websites they measure
> We now monitor changes against a test suite of approximately 25 > websites in order to guide V8 optimization. In addition to the > aforementioned websites and others from the Alexa Top 100, we selected > sites which were implemented using common frameworks (React, Polymer, > Angular, Ember, and more), sites from a variety of different geographic > locales, and sites or libraries whose development teams have > collaborated with us, such as Wikipedia, Reddit, Twitter, and webpack. > We believe these 25 sites are representative of the web at large and > that performance improvements to these sites will be directly reflected > in similar speedups for sites being written today by JavaScript > developers.
Obviously they would want to test Facebook because a lot of people use that website. I'm sure they tested many others as well.