3 ms·
You get something similar to with static methods and properties on class definitions. In plain ES2017-ish class syntax: class Test { static value = 1; st
by a_humean 9y ago
You get something similar to with static methods and properties on class definitions. In plain ES2017-ish class syntax:
class Test {
static value = 1;
static someStaticMethod = () => {
return 5;
}
}
console.log(Test.value) // 1
console.log(Test.someStaticMethod()) // 5
Edit: Sorry, this actually a Stage 3 TC39 feature. Not sure if you can use it with flowtype (I think you can), but you might be able to if you enable babel with stage-3 features:
https://github.com/tc39/proposal-class-fields https://github.com/tc39/proposal-class-fields
You don't actually need this fancy class syntax anyway. You can just define a class and then do:
Test.value = 5
Test.someStaticMethod = () => {
return 5;
}
The class stuff is just sugar over the prototype stuff anyway, and a class is just a constructor function object with a prototype chain.
- pcx 9y agoYeah, either way it is objects. Traits feel so much more flexible though; and more natural a layer over JS object-orientedness. It also makes it a pain that there are some important semantics missing while using classes in JS. Someitmes Java feels much better.
- akatechis 9y agoThe real problem is that ES classes map onto a paradigm of prototypal inheritance rather than traditional inheritance (as in Java). The discrepancies between the two cause a leaky abstraction, like how a class still has a prototype chain, for example.
- fish_fan 9y agoNote, I think in spite of being in stage 3 the syntax and obvious cases seem to be largely unlikely to change. It certainly improves react quite a bit: you can define the proptypes and default values inside the class rather than after.