0

I saw on one youtube video that he is making getter and setter for a property in the App Component.


 private number: number = 23;

  get counter() {
    return this.number;
  }

  set counter(value) {
    this.number = value;
  }

  increment() {
    this.counter++;
  }

  decrement() {
    this.counter--;
  }

Why would we make something like this when we still can access the private and the public property in the HTML.

{{ number }}
  • 1
    Does this answer your question? [Why use getters and setters/accessors?](https://stackoverflow.com/questions/1568091/why-use-getters-and-setters-accessors) – Christopher Peisert Oct 15 '20 at 13:39
  • No, because there you can't use the field outside, but here i can use in the HTML even it is private –  Oct 15 '20 at 13:41
  • The provided answer is for JAVA.I need help to understand why in ANGULAR i can access somethung that is private outside in the HTML and the advantage of using it –  Oct 15 '20 at 13:43
  • See [Angular2 - should private variables be accessible in the template?](https://stackoverflow.com/q/34574167/1164465) – Christopher Peisert Oct 15 '20 at 13:43
  • not sure what version of angular you are using, but current versions do not allow access to private members. proof: https://stackblitz.com/edit/angular-ivy-lbmurz – Markus Dresch Oct 15 '20 at 13:48
  • I have angular 9 –  Oct 15 '20 at 13:49
  • 1
    angular 9 does not allow access to private members as well (further proof: https://stackblitz.com/edit/angular-ivy-4bzqlj?file=package.json). and for the "JAVA" answer: the reason for using getters/setters is the same in any language, be it JS, TS, Java, C# or any other language that has the feature. – Markus Dresch Oct 15 '20 at 13:57
  • not sure, but maybe the rule is relaxed for some obscure reason in dev mode. pretty sure you at least get an error when trying to build for prod. – Markus Dresch Oct 15 '20 at 14:00
  • Thanks, btw what is the advantage of using getters and setters –  Oct 15 '20 at 14:01
  • "*what is the advantage of using getters and setters*" -> [Why use getters and setters/accessors?](https://stackoverflow.com/q/1568091) – VLAZ Oct 15 '20 at 14:03

2 Answers2

0

Sometimes, the value you want stored in your property requires some computation. In that case, you can do:

class A {
  private _fieldLength: number = 0;
  get fieldLength(): number { return this._fieldLength; }
  set fieldLength(val: string): void { this._fieldLength = val.length; }
}

const a = new A();
console.log(a.fieldLength) // 0
a.fieldLength = "Hello"
console.log(a.fieldLength) // 5

In the example you used, it reaaly is easier to just use a public property. When you need some computation, using a getter/setter can be better.

Askirkela
  • 1,120
  • 10
  • 20
0

This is not a recommended way. This will cause a performance problem. It is technically an impure pipe. Please do your best to avoid this way.

Here you can check the material - https://angular.io/api/core/Pipe#pure

Liu Zhang
  • 1,808
  • 19
  • 31