2023-05
function minusTwo({ set }) {
return {
set(v) {
set.call(this, v - 2)
},
init(v) {
return v - 2;
}
}
}
function timesFour({ set }) {
return {
set(v) {
set.call(this, v * 4)
},
init(v) {
return v * 4;
}
}
}
class Foo {
@minusTwo @timesFour accessor bar = 5;
}
const foo = new Foo();
console.log(foo.bar); // 18
foo.bar = 5;
console.log(foo.bar); // 12
Reverse the order of initializers, run them from outermost -> innermost
class Foo {
@minusTwo @timesFour accessor bar = 5;
}
// 5 - 2 * 4 = 12
const foo = new Foo();
console.log(foo.bar); // 12
foo.bar = 5;
console.log(foo.bar); // 12
Restore the previous behavior, have setters called with initial value for auto-accessors only
class Foo {
@minusTwo @timesFour accessor bar = 5;
}
// Initial value = 4 * 5 - 2 = 18
// Set value = Initial value - 2 * 4 = 64
const foo = new Foo();
console.log(foo.bar); // 64
foo.bar = 5;
console.log(foo.bar); // 12