Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
56
rated 0 times [  59] [ 3]  / answers: 1 / hits: 78856  / 9 Years ago, thu, august 13, 2015, 12:00:00

What does get mean in this ES6 class? How do I reference this function? How should I use it?



class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}

get area() {
return this.calcArea()
}

calcArea() {
return this.height * this.width;
}
}

More From » methods

 Answers
40

It means the function is a getter for a property.



To use it, just use it's name as you would any other property:





'use strict'
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}

get area() {
return this.calcArea()
}

calcArea() {
return this.height * this.width;
}
}

var p = new Polygon(10, 20);

alert(p.area);




[#65420] Tuesday, August 11, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bradenc

Total Points: 75
Total Questions: 96
Total Answers: 129

Location: Burundi
Member since Thu, Feb 10, 2022
2 Years ago
;