-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.ts
30 lines (29 loc) · 847 Bytes
/
5.ts
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
/**
* 封装变化
*/
export { createInstance };
interface Window {
hello: any;
}
function Window() {}
Window.prototype.hello = function () {
console.log('hello');
};
// 希望 封装变化,希望这个createInstance 可以创建任何类型的实例
let createInstance = function <T extends new () => T>(Constructor: T) {
let instance: any;
return function AnyConstructor(this: any, ...args: []) {
if (!instance) {
// 正常来说 this.__proto = AnyConstructor.prototype
Constructor.apply(this, args);
// this.__proto = Constructor.prototype
Object.setPrototypeOf(this, Constructor.prototype);
instance = this;
}
return instance;
};
};
let createWindow = createInstance(Window as any);
let w1 = new (createWindow as any)();
let w2 = new (createWindow as any)();
// console.log(w1 === w2);