在NodeJS中调用ES6callback中的基类
例如,我有以下类:
class X extends Y { constructor() { super(); } method() { asyncMethod( function( err ) { super.method( err ); } ); } }
但是, super
不是基类Y
我怎么能通过super
callback?
有没有比使用箭头function有任何解决scheme?
一个可能的解决scheme是使用箭头function,例如,
asyncMethod( err => super.method( err ) );
我肯定会去箭头function,但是,如果你需要一个替代品,在这里。
根据JavaScript闭包行为,你可以将一个对 super.method
的引用存储 在一个variables中 ,并在callback中使用它。
代码如下:
class X extends Y { constructor() { super(); } method() { let superMethod = super.method; asyncMethod(function (err) { superMethod(err); }); } }