我理解thisjavascript中的关键字.我已经用它喜欢this.method()或this.variable =.但这是什么().请参阅以下代码:
static fromTX(tx, index) {
return new this().fromTX(tx, index);
}
Run Code Online (Sandbox Code Playgroud)
请帮助我理解在javascript和上面的代码示例中使用this().
在静态方法内部,this将引用构造函数,因此new this()将调用构造函数:
class Foo {
constructor() {
console.log('making instance');
}
static makeFoo() {
return new this();
}
}
const f = Foo.makeFoo();Run Code Online (Sandbox Code Playgroud)
当然,this只有在this引用函数时才可以调用,否则会引发错误.通常,this将引用一个对象,而不是一个函数.
| 归档时间: |
|
| 查看次数: |
152 次 |
| 最近记录: |