例如,我有以下课程:
class X extends Y
{
constructor() { super(); }
method() {
asyncMethod( function( err ) {
super.method( err );
} );
}
}
但是,super
不是基类Y
。如何将super
传递给该回调?
有没有使用箭头功能的解决方案?
答案 0 :(得分:2)
一种可能的解决方案是使用箭头功能,例如
asyncMethod( err => super.method( err ) );
答案 1 :(得分:2)
我肯定会选择箭头功能,但是,如果你需要替代方案,那就可以了。
根据JavaScript闭包行为,您可以在变量中存储对 super.method
的引用,并在回调中使用它。
以下是代码:
class X extends Y
{
constructor() { super(); }
method() {
let superMethod = super.method;
asyncMethod(function (err) {
superMethod(err);
});
}
}