当我们想要替换构造函数时,我试图理解类装饰器如何在Typescript中工作。我见过这个演示:
const log = <T>(originalConstructor: new(...args: any[]) => T) => {
function newConstructor(... args) {
console.log("Arguments: ", args.join(", "));
new originalConstructor(args);
}
newConstructor.prototype = originalConstructor.prototype;
return newConstructor;
}
@log
class Pet {
constructor(name: string, age: number) {}
}
new Pet("Azor", 12);
//Arguments: Azor, 12
一切都被理解,但这一行:
newConstructor.prototype = originalConstructor.prototype;
为什么我们这样做?
答案 0 :(得分:2)
类如:
class Pet {
constructor(name: string, age: number) {}
dosomething() {
console.log("Something...");
}
}
在定位ES5时编译成函数:
var Pet = (function () {
function Pet(name, age) {
}
Pet.prototype.dosomething = function () {
console.log("Something...");
};
return Pet;
}());
当我们使用函数来定义类时,你会发现。这些方法被添加到函数的原型中。
这意味着如果要创建新的构造函数(新函数),则需要从旧对象复制所有方法(原型):
function logClass(target: any) {
// save a reference to the original constructor
const original = target;
// a utility function to generate instances of a class
function construct(constructor: any, args: any[]) {
const c: any = function () {
return constructor.apply(this, args);
};
c.prototype = constructor.prototype;
return new c();
}
// the new constructor behaviour
const newConstructor: any = function (...args: any[]) {
console.log("New: " + original.name);
return construct(original, args);
};
// copy prototype so intanceof operator still works
newConstructor.prototype = original.prototype;
// return new constructor (will override original)
return newConstructor;
}
您可以在"Decorators & metadata reflection in TypeScript: From Novice to Expert (Part I)"
了解详情请参阅https://github.com/remojansen/LearningTypeScript/tree/master/chapters/chapter_08了解更新版本。