Implementing Private Fields for JavaScript
hacks.mozilla.org
hacks.mozilla.org
Are there any obvious advantages as vast better JIT performance, etc
Edit: Sounds very confusing and overengineered
> 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 wanted to make the language more friendly to CS graduates and Java developers who are otherwise not comfortable using the language without the OOP they cannot live without outside this language. As an example, when I have had discussions about this in the past most proponents had no idea how scope works in the language.
With that said, JS is a mess in a lot of ways, so maybe this is worthwhile. Hard to know though until it’s too late to revert.
Just my opinion though, I’m curious what others think about this and where I may be missing the mark.
2. Java (and c++) are great examples of class-oriented languages. Javascript is object-oriented.
And here's the kicker: The freestanding variables that you do declare inside a factory function, those are naturally private fields. You can expose it to the outside world via method closures, but no one else can reach in and modify it directly. Classes can't do that. Functions could. But the general sentiment is to embrace classes and throw factory functions to the wayside, and as a result you need extra effort like implementing private fields for classes when JavaScript functions can already do the same thing.
function NewObj(n) {
let myPublicInt = n;
let myPrivateInt = 0;
function privateIncrement() { myPrivateInt++; }
function incrementPrivateInt() { privateIncrement(); }
function printPrivateInt() { console.log(myPrivateInt); }
return {
myPublicInt,
incrementPrivateInt,
printPrivateInt,
};
}
let obj = NewObj(5);
console.log(obj.myPublicInt); // 5, because myPublicInt is a public field
console.log(obj.myPrivateInt); // undefined, because myPrivateInt is a private field
// obj.privateIncrement(); // exception, because privateIncrement is a private method
obj.printPrivateInt(); // 0
obj.incrementPrivateInt();
obj.printPrivateInt(); // 1And it's still a pretty ugly way of defining a class like object without the syntactics.
And if anything happens that does "deoptimize" it, then if we know the "base" is definitely a class, the paths to handle the "deoptimization" can make better decisions and be able to avoid scrapping everything and falling back to the slow path.
How so? I have no trouble reading that, it's certainly clearer than most of the c++ templates I've seen, and error prone? For example?
One of the problems of the style is that you can't easily split it up. Another one is that it allows you to redeclare/reassign x. A third one is that it doesn't tell the reader what it's doing. You'd have to look carefully to find that out. And then there's the "extends" functions, which everyone implements slightly differently. So, for me, the "class" keyword is a positive change that helps overcome JavaScript's dynamic nature a bit.
> like it's a good thing
I think so. If you have full control over your own code, and you write in e.g. TypeScript, you don't need it. Otherwise, it can prevent errors that take forever to debug.
TypeScript, also unnecessary and often just a pita.
I've written very much javascript and have never had a bug that took "forever to debug'.
Programmers who like classes, and type-checking, and all the other syntactic sugar should, imo, just use languages that support such things and leave javascript alone.
I learned a lot about programming from the SICP when it first came out. I appreciate and use the good things Scheme inspired in javascript. And with very rare exceptions, such as const, let, and enhancements to builtin objects such as arrays, I still use vanilla js. I create new objects using factory functions and have zero use for classes.
However adding Map, Set, and similar objects to what is essentially the javascript library is fine with me as they do not impact the core syntax of js.
I'd say, they were right.
I don't really get it either, and I am saying this as someone who codes in both JS and Java. While thinking in OOP concepts comes naturally for me, I found the prototype/function based approach of JS a breeze of fresh air.
I guess the turning point was when JS migrated to the backend and more complex business logic was written in JS. As long as you are basically rendering view state or filtering data, traditional JS is much easier. But write the business logic of some enterprise app, and you want to use the OOP paradigms.
For example, I did, what every decent JS developer back then did: you build yet another library ("helper library"), that just abstract these creations away.
So what else is the Class syntax? A helper.
I felt I was one of the very, very rare JS devs, who read "Secrets of a JS Ninja" from John Resig, the jQuery legend, top to bottom multiple times. Afterwards I build very cool stuff, however folks did not understand it with their normale knowledge of JS. It blew me away, that you could do stuff like Function.toString() in combination with RegEx and New Function(). I was awesome, that I could do, what the C# and Java guys could do. I just took a lot more code to achieve. Fast forward 10 years.
I am extremely glad, that TypeScript came a long. It makes collaboration so much more meaningful.
I still love the JavaScript ECMA5 stuff, however the tradeoff was too big. It is like staying with C instead of Python, Java, or DSLs.
Even "You might not need jQuery" ended up being pointless in regards to readability. Why write 20 lines of code, when you can put it in one line? Paradoxically Resig's jQuery had to do a lot of complex stuff to achieve the beauty of $(). So why not simply write Class?
Just my opinion.
https://github.com/tc39/proposal-private-fields/blob/master/...
Edit: wrt encapsulation, will the proposed solution work when a class marks the same field private as the one it extends?
Before the private fields, it was possible to create private properties through a WeakMap object which is somewhat clumsy and not very performant.
They shouldn’t be used or implemented.
A waste of time and effort.
I suspect there’s a risk that if the committee standardised on _ rather than #, it would actually break a load of older apps and content. There’s a good chance the _convention was abused in those earlier codebases.
https://github.com/tc39/proposal-private-fields/blob/master/...