示例:
class Person {
constructor(name) {
}
get name() {
return /* “name” argument from constructor */;
}
}
我希望 name
getter 返回传入构造函数的 name
值。
最佳答案
将参数保存到实例属性:
class Person {
constructor(name) {
this.name_ = name;
}
get name() {
return this.name_;
}
}
关于javascript - 如何从 ES6 类中的 getter 函数返回构造函数参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32039457/