ES2022 feature: class static initialization blocks
2ality.com
2ality.com
That being said I never though that JS was really lacking syntax-wise. "Arrow functions" are a nice shorthand for sure, and "let" should really have been there from the start, but this addition of classes feels really unnecessary and redundant. It just creates more ways to do things you could already do, without obvious benefits and the obvious drawback of making the language more complex.
Since nowadays transpilation is so common place, what even is the point? You already have a rather large selection of other languages to use if you don't like raw JS.
I'm all for improving the existing JavaScript language but that just looks like tacking on a completely different language on top of it in order, I assume, to make it look more familiar for Java/C# coders?
Many of them have some support for async/await but few can handle all cases such as async-await within try-catch and so on.
Scala.js async-await is not part of the standard libary I think and has this limitation: https://github.com/scala/scala-async#limitations.
Kotlin.js might support async-await but I find their JavaScript documentation so confusing/non-existent I can't tell. But also, Kotlin is too similar to JavaScript already anyway. It doesn't have ML-style pattern matching for example.
The Java-to-JavaScript and C#-to-JavaScript compilers both are too underused as far as I can tell. The latter, Blazor, is too focused on ASP.NET for me to be able to understand if I could write it the way I write React apps.
Elm, ReScript, and other ML/Haskell-influenced languages are too niche even though I'm a big Standard ML fan.
I'm not sure what else there is to look at.
And I really don't want to go back to using promises and callbacks.
(Also note, => is not just a different syntax for function)
2006 era JS was more than sufficient to build out highly maintainable 100k+ LOC codebases, all this stuff does is introduce new variations of the same functionality for almost no benefit. Javascript is no longer a small language. I think the only surviving practical small language is Lua, and it's only remained small because the hoards are yet to turn up and bulldoze its original design with infinite feature requests
Typescript wouldn't have been created if this was the case.
I'm already fighting with people about integer literal separators not being supported in e.g. Vue and stuff, the more that's added, the more the community becomes fragmented, and I'm not convinced that transpiration (I hate that word...) is really paying off for us after all these years.
I don't blame them. That's exactly how I would feel!
That, or, it's easier to upgrade to a new JS version + let your transpiler figure it out, than to rewrite your code in some other language that compiles to JS.
Instead of trying to turn JavaScript into C#/Java, C# and Java should just support compiling to web assembly and provide a good standard library for working with browsers. /rant
A specific example of this is doing some small custom callback logic in d3 configuration.
foo: {
const f = 1;
f
}
Versus: foo: (() => {
const f = 1;
return f;
})()
Blocks are common in other expression-oriented languages like Scheme, Ruby, Rust, and Standard ML. I would like to be able to use that in JavaScript. foo: do {
const f = 1;
f
}
(because a plain block would be ambiguous with object literal syntax in some cases). let x
_: {
let y = 1
let z = 2
x = y + z
}
y // <- throws function Foo () {
var x = 1 // "static initialization block"
return {
y: 2,
z: function () { return x + y }
}
}
const foo = Foo()
There's been exactly zero need for the "class" keyword, "constructor()", or that new "#private" thing.There is one flaw in the design of the language which opens the door to all this nonsense; that's having to write "foo.method.bind(foo)" instead of just "foo.method" when passing "method" as a callback, in order to preserve the value of "this". Although in the example above this is not a problem, as you can write functional code that passes the context explicitly without needing "this".
Another thing I wish JavaScript had kept was the "with" statement.
I can agree that classes have been more or less a syntactic sugar, but private identifiers are not. The field name collision is a problem in virtually every dynamically typed language with namespace inheritance and each language has their solutions (see Python `__mangled_name` and Ruby `@instance_var`), JavaScript just happens to have another solution and that alone justifies a separate `class` syntax (since the scope of such private identifiers should be lexical).
Remember, once upon a time JS was supposed to be Scheme...
Your approach can't translate the following code:
class MyNumber {
#value;
constructor(value) { this.#value = value; }
add(other) { return new MyNumber(this.#value + other.#value); }
}
The proper solution is to use a non-enumerable property with an unnamed Symbol name, which is possible but cumbersome.Consider the following:
with (a) {
with (b) {
with (c) {
d = e;
}
}
}
Does this mean:(1) a.b.c.d = e
(2) d = a.b.c.e
(3) something else
(4) it's impossible to tell?
Correct answer: (4) it's impossible to tell!
One of the worst things about programming is looking at someone else's code (or worse, one's own code) and thinking "WTF did they mean by that?". And that's why I don't like the with statement.
Thus, TS should not have any influence over how JS evolves, but unfortunately that ship has sailed it seems.
Javascript library maintainers should not be berated like they (we) are when types are not added to libraries, for example, but these days it's an unspoken requirement.
Typescript community manages external typings for tons of libraries that don't want to support ts themselves. People can be referred to create type definitions themselves under DefinitelyTyped.
[0] https://docs.github.com/en/github/site-policy/github-communi...
Their bullying and harassment guidelines are very clearly toward ad-hom types of remarks and beyond.
Being on the receiving end of a dick comment about how not having typescript types is unacceptable and how I need to have them for one reason or another is never fun - but, assuming they don't personally attack me as an individual, I'll fight tooth and nail for them to make those comments as it pertains to the community guidelines.
That's not to say I appreciate them or let them influence my maintainership, but they shouldn't be disallowed or cause for punitive action by any means.
Deep paths look interesting on top of this, the idea is one I like but not sold on it being implementable in any sort of non-clunky way.
https://github.com/tc39/proposal-deep-path-properties-for-re...
class Foo {}
const r = func()
Foo.prop1 = r.prop1()
Foo.prop2 = r.prop2()
What am I missing? Why do we need all these new syntaxes? class Translation{
static get translations(){
return {blah: things};
}
static englishWords(){ return Object.keys(this.translations);}
}
Since it's all static can't browsers optimize what's already there in this case? I feel like I'm missing something. const t = new Translation
// property value that
// is initialized once
t.translations === t.translations
// getters which return
// new objects
t.translations !== t.translations
so at the very least the browser can't optimize that behavior away, it needs to create new objects. Besides, ensuring that say, call to `Object.keys` is side-effect free is challenging.If we're talking only about static properties it seems more reasonable to me to freeze them upon definition or first use.
> it seems more reasonable to me to freeze them upon definition or first use
I agree, and that's why I'd default to
static translations = {blah: things}
and not static get translations() {
return {blah: things}
}
the latter does not "freeze" themI have started migrating from those platforms to ruby / clojure. since things rarely get added to those ecosystems. one can pop in after 20 years and nothing has changed and everything still works as before
20 years ago Clojure didn't exist and Ruby was practically unknown outside of Japan
JS in Spring
Lotus blossoms
Java clouds rain
All is lostId like to see more functional programming concepts implemented before more OOP.
JS has things like String,Number,Date why not use classes to create your own stuff (say Customer,Product, Transaction) and have your pure functions operate and return well defined objects - for sure it helps with readability and maintainability.
There are still a couple issues that have to be solved. m2c: I hope that won't get to stage 3, though I like operator overloading in general, I think it's something that will complicate JS even more.