4 ms·
The example with the Person class suggests that the author is unaware of JavaScript getters: class Person { get isImmortal() { return false; // no
by azangru 9y ago
The example with the Person class suggests that the author is unaware of JavaScript getters:
class Person {
get isImmortal() {
return false; // noone is immortal
}
}
p = new Person();
p.isImmortal
// >>> false
p.isImmortal = true;
p.isImmortal
// >>> false
I mean, even plain untyped JavaScript can do that!
- objectiveariel 9y agoYou're missing the point. The fact that the language still allows you to write `p.isImmortal = true;` is the issue.
- coldtea 9y agoWhich won't do anything. p.isImmortal will still be the setter. So it's a non issue -- and the linter can catch the mistake anyway.
- oceanswave 9y agoAnd my linter, or flow, typescript will pick that up in the same way the compiler will. Just because tooling these days catches these things in 'type safe' languages doesn't mean it's not a pointer to a chunk of memory that's defined at runtime beneath it all.
- raquo 9y agoThat was a bad example indeed. If isImmortal was accepting any params though, you wouldn't be able to make it a getter. And sure, Object.freeze exists, but almost none of the JS code I've seen uses it. And what's the point really when assignments to frozen fields don't even throw an exception, just fail silently. Same for getters. None of those tricks are gonna make JS into a good language in the eyes of people who don't for whatever reason consider it good already.