我想知道为什么我无法访问参数' this.name'在方法的装饰者中。
我的代码:
class A {
name: string;
construct() {
this.name = 'test';
}
@decorators(this.name)
method() {}
}

在我的装饰师中,我得到一个未定义的。 我错过了一些东西,我没有得到它。 你能救我吗? 谢谢
答案 0 :(得分:0)
您从对象语法开始:var C = { name: string; }
我为您创建了一个代码片段,用于创建类并使用extend函数。
在评论中询问ES5的解释。
class A {
constructor(param1, param2) {
this.param1 = param1;
this.param2 = param2;
}
// create a function
get1() {
return this.param1
}
set1(param1) {
this.param1 = param1;
}
}
class B extends A {
constructor(param1, param2, param3) {
super(param1, param2); // Call A's constructor via super
this.param3 = param3;
}
get3() {
return this.param3
}
set3(param3) {
this.param3 = param3;
}
}
let a = new A(10, "foo"),
b = new B(5, "doo", "lala");
console.log("--- A ---");
console.log("a.get1() : " + a.get1()); // read date from a
a.set1(3); // set a.param1 to 3
console.log("a.get1() : " + a.get1()); // read date from a
console.log("a.param2 : " + a.param2); // read date from a
a.param2 = 15; // set a.param2 to 15
console.log("a.param2 : " + a.param2); // read date from a
// add a variable to the a object
a.newvar = "new variable a";
console.log(a.newvar);
// add a function to the a object
a.newfunc = (param) => {
return param;
}
console.log(a.newfunc("function a"));
console.log("--- B ---");
console.log("b.get1() : " + b.get1()); // read date from b
b.set1(3); // set b.param1 to 3
console.log("b.get1() : " + b.get1()); // read date from b
console.log("b.param2 : " + b.param2); // read date from b
b.param2 = 15; // set b.param2 to 15
console.log("b.param2 : " + b.param2); // read date from b
console.log("b.get3() : " + b.get3()); // read date from b
b.set3(3); // set b.param3 to 3
console.log("b.get3() : " + b.get3()); // read date from b
// the newvar is not given with object b
console.log(b.newvar);
// add a variable to the b object
b.newvar = "new variable b";
console.log(b.newvar);
// add a function to the b object
b.newfunc = (param) => {
return param;
}
console.log(a.newfunc("function b"));