Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 23 additions & 27 deletions docs/decorator.md
Original file line number Diff line number Diff line change
Expand Up @@ -746,42 +746,38 @@ accessor 装饰器的`value`参数,是一个包含`get()`方法和`set()`方
下面是一个例子。

```typescript
class C {
@logged accessor x = 1;
}

function logged(value, { kind, name }) {
if (kind === "accessor") {
function logged<T = any>(value: ClassAccessorDecoratorTarget<unknown, T>, context: ClassAccessorDecoratorContext) {
if (context.kind !== "accessor") {
throw new TypeError("@logged 只能用于修改 acessor")
}
let { get, set } = value;

return {
get() {
console.log(`getting ${name}`);
get() {
console.log(`getting ${String(context.name)}`);
return get.call(this)
},
set(val: any) {
console.log(`setting ${String(context.name)} to ${val}`);

return get.call(this);
},
return set.call(this, val);
},

set(val) {
console.log(`setting ${name} to ${val}`);
init(initialValue: any) {
console.log(`initializing ${String(context.name)} wiht value ${initialValue}`);

return set.call(this, val);
},

init(initialValue) {
console.log(`initializing ${name} with value ${initialValue}`);
return initialValue;
}
};
}
return initialValue
}
}
}

let c = new C();

c.x;
// getting x
class C {
@logged accessor x = 1;
}

c.x = 123;
// setting x to 123
const c = new C();
c.x; // "getging x"
c.x = 123; // "setting x to 123"
```

上面示例中,装饰器`@logged`为属性`x`的存值器和取值器,加上了日志输出。
Expand Down