3 ms·
This sounds very arbitrary, opinionated, and unconvincing. If a class is a co-location of data and methods performed on it, then how is this class: class Foo
by azangru 7y ago
This sounds very arbitrary, opinionated, and unconvincing. If a class is a co-location of data and methods performed on it, then how is this class:
class Foo {
constructor(value) {
this.value = value;
}
addOne() {
this.value++;
}
getValue() {
return this.value;
}
}
const foo = new Foo(1);
any worse than this POJO:
const foo = {
value: 1,
addOne() {
this.value++;
},
getValue() {
return this.value;
}
}
Besides, what are models and controllers in React codebases, and why is using classes for one of these groups any better than for the other?
- neurotrace 7y agoI think you missed the part where they said you don't need classes for POJOs and models i.e. things which only contain data and do nothing else. If you want methods that operate against the inner data of an object then totally write classes
- azangru 7y agoIt’s just that they started their post by saying that classes in Typescript should be avoided. A message "x should be avoided" is very different from "you don’t need x". Compare the statement "you don’t need redux", which has been incorporated into titles of numerous blog posts, with the statement "redux should be avoided".
- Roboprog 7y agoThe fact that you wrote a function called “getValue” screams “Java programmer!”. Ordinary js, like C# and some other languages, has accessors so you just use the property name as an r-value (or l-value). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- hombre_fatal 7y agoWhy pick on a contrived example? You can fill in your own less-contrived state + methods, it's not really the point of their question.
- Roboprog 7y agoAs an example of the “OOP according to Java (and C++)” culture that is making JavaScript tedious to work in. I want move past the “bad OOP” stuff shoved down my throat 30 years ago, into a style that is more FP oriented.