【发布时间】:2016-08-29 05:05:55
【问题描述】:
打字稿代码
@logClass // this is my decorator
class Person {
public name: string;
public surname: string;
constructor(name : string, surname : string) {
this.name = name;
this.surname = surname;
}
}
function logClass(target: any) {
// save a reference to the original constructor
var original = target;
// a utility function to generate instances of a class
function construct(constructor, args) {
var c : any = function () {
return constructor.apply(this, args);
}
c.prototype = constructor.prototype;
return new c();
}
// the new constructor behaviour
var f : any = function (...args) {
console.log("New: " + original.name);
return construct(original, args);
}
// copy prototype so intanceof operator still works
f.prototype = original.prototype;
// return new constructor (will override original)
return f;
}
在上面的代码中,新的构造函数添加了一个额外的console.log();。但我也想更改传递给构造函数的参数数量。可能吗?或者我们可以添加任何其他行为吗?如果有,请告诉我?
【问题讨论】:
标签: typescript