Hacker News new | ask | show | jobs
by azangru 2491 days 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?
1 comments

I 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
It’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".