【发布时间】:2016-11-04 18:18:49
【问题描述】:
我正在尝试从一组类中创建对象,而不必定义每个类...基本上我正在尝试创建装饰器模式。在打字稿中,由于编译限制,这似乎几乎是不可能的。
我尝试过使用代理。没有骰子。
这是我想要完成的用法(缺少一些代码来允许我正在尝试做的事情 - 这是我想要解决的问题)。
class Person {
public name:string;
public age:number;
public identify(){
console.log(`${this.name} age ${this.age}`);
}
}
class Child {
public Mother:Person;
public Father:Person;
public logParents(){
console.log("Parents:");
this.Mother.identify();
this.Father.identify();
}
}
class Student {
public school:string;
public logSchool(){
console.log(this.school);
}
}
let Dad = new Person();
Dad.name = "Brad";
Dad.age = 32;
let Mom = new Person();
Mom = new Student(Mom);
Mom.name = "Janet";
Mom.age = 34;
Mom.school = "College of Night School Moms";
let Johnny = new Person();
Johnny = new Child(Johnny);
Johnny = new Student(Johnny);
Johnny.name = "Johnny";
Johnny.age = 12;
Johnny.Mother = Mom;
Johnny,Father = Dad;
Johnny.school = "School for kids who can't read good";
【问题讨论】:
标签: javascript typescript proxy decorator