ts 装饰器 class Minix

下次继续。。。

// https://blog.csdn.net/qq_38765789/article/details/103201602

function 拷贝属性(target, source) {
  for (let key of Reflect.ownKeys(source)) {
    // name,constructor,prototype之外属性就是实例属性
    if (key !== "constructor" && key !== "prototype" && key !== "name") {
      // 返回某个对象属性的描述对象( descriptor )。 参数(对象, 属性)
      let desc = Object.getOwnPropertyDescriptor(source, key);
      Object.defineProperty(target, key, desc);
    }
  }
}

function func(...a: Function[]) {
  return (constructor: Function) => {
    console.log("constructor.prototype ", ">", constructor.prototype, "<");

    console.log("...a ", ">", ...a, "<");

    const mix = {
      say() {
        console.log("我佛了");
      },
    };

    for (const clz of a) {
      console.log("clz -- ", Reflect.ownKeys(clz.prototype));
    }

    Object.assign(constructor.prototype, mix);

    constructor.prototype.boom = () => console.log("boom");
  };
}

class ServicA {
  say() {
    console.log("说说说");
  }
}

class ServicB {
  run() {
    console.log("跑跑跑");
  }
}

@func(ServicA, ServicB)
class AAA {}

const a = new AAA();

console.log("--------------------");
(a as any)?.boom();
(a as any)?.run();
(a as any)?.say();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56