0

What is the purpose of get and set keyword in ES6. I have seen the below given example. Code Example. This could be done without using the get and set keyword so why we use it.

get area() {
    return this.height * this.width;
  }

  set area(value) {
    this.area = value;
}
Jitender
  • 7,593
  • 30
  • 104
  • 210

1 Answers1

1

Introducing the class syntax and getter and setters are just an evolution of the language. Getters and setters are also possible with es5. But like the class syntax it helps developers coming from the Java world to better grasp the language. Basically you may view it as the language maturing. Al though a controversial topic. The industry is now split into those who decide to use the syntactic sugar and those who do not. I use both depending who I am coding for.

Oz Lodriguez
  • 965
  • 5
  • 16
  • 1
    Actually, syntax did *not* change. You could use exactly the same in ES5 as well, only `class` is new. – Bergi Jul 07 '16 at 06:26