4 ms·
Because syntactic sugar matters. var x = (function(){})() is just not readable, but it is error prone. The same goes for assigning to __proto__. IMO, the class
by tgv 5y ago
Because syntactic sugar matters. var x = (function(){})() is just not readable, but it is error prone. The same goes for assigning to __proto__. IMO, the class keyword is a step forward. However, adding # to a variable looks like a step backward. How long before we have this.%x, to mark x "protected"? Or get x() {...} vs set #x()?
- SPBS 5y agoFactory functions don't mean using the (admittedly ugly) IIFE syntax or accessing __proto__ fields. This is an example of factory function 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(); // 1
- tehbeard 5y agoso..... how do you know to optimize this as a factory function / "class" in your JS runtime with reusable instructions that just use an instance's scope in order to get good performance and memory usage.... When all that's being spat out is a JS object, that the user could very well modify in the next instruction, 30 instructions time, or conditionally based on some other checks? And it's still a pretty ugly way of defining a class like object without the syntactics.
- hinoki 5y agoBut an object constructed from a class still has all the optimisation problems you just mentioned. It’s just syntactic sugar. The optimiser generates invariants which, if invalidated, makes you fall back to a slow path.
- tehbeard 5y agoThe syntactic sugar is something the optimiser is aware of and isn't just a "do this in a more pretty/stylised way" for the developer, it conveys an intent that this is a construct that can be optimized with a certain level of confidence that you can't get from objects that "look like a duck and quack like a duck" if you squint a little. 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.
- felixfbecker 5y agoJavaScript engines like V8 can already do exactly as you said through hidden classes based on source code position and have been doing so for a long time. I haven't read any evidence that classes are actually better optimized, even if it's often argued as the reason for why classes are better.
- zanethomas 5y ago"var x = (function(){})() is just not readable, but it is error prone." 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?
- tgv 5y agoIt's a bit apples and oranges. That declaration doesn't have the power of a C++ template. A C++ class is much simpler. 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.
- zanethomas 5y agoconst x = ... it's usual to read code to understand it "helps overcome JavaScript's dynamic nature", you say that like it's a good thing
- tgv 5y agoWeren't class and const introduced in the same version? > 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.
- zanethomas 5y agoThe utility of const does nothing to support adding classes to javascript. 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.