【发布时间】:2022-01-18 04:22:23
【问题描述】:
get 在这个 ES6 类中是什么意思?我如何引用这个函数?我应该如何使用它?
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
get area() {
return this.calcArea()
}
calcArea() {
return this.height * this.width;
}
}
【问题讨论】:
-
它很可能只是一个getter,但在一个类而不是一个对象中。它并不是真正的 ES6 特定的。
-
@Xufox 你的意思是它不是 ES6 特定的?
-
@KeithNicholas:它在 ES5 中也一样。
-
@KeithNicholas Getters 自 ES5 以来就存在,我认为。这里唯一的 ES6 是
class语法,但 getter 并不是什么新鲜事。
标签: javascript methods getter