【发布时间】:2018-06-30 05:14:19
【问题描述】:
任何想法如何使用装饰器将类字段转换为 getter/setter?示例:
class Foo:
@accessor bar = 0;
const foo = new Foo;
应该在 foo.bar = 1; 上表现出自定义行为
我已经尝试过类似的东西
function accessor(target, name, descriptor) {
let val;
return {
set: function(newVal) {
val = newVal;
console.log("setter called");
},
get: function() { return val; }
};
}
但这会丢失bar = 0的初始值。
【问题讨论】:
标签: javascript babeljs ecmascript-next