Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在NodeJS中ES6的回调中调用基类_Javascript_Node.js_Ecmascript 6 - Fatal编程技术网

Javascript 在NodeJS中ES6的回调中调用基类

Javascript 在NodeJS中ES6的回调中调用基类,javascript,node.js,ecmascript-6,Javascript,Node.js,Ecmascript 6,例如,我有以下课程: class X extends Y { constructor() { super(); } method() { asyncMethod( function( err ) { super.method( err ); } ); } } 但是,super不是基类Y。如何将super传递给该回调 除了使用箭头功能,还有其他解决方案吗?一种可能的解决方案是使用箭头功能,例如 asyncMethod(

例如,我有以下课程:

class X extends Y
{
   constructor() {  super(); } 

   method() {
      asyncMethod( function( err ) {   
          super.method( err );
      } );
    }
}
但是,
super
不是基类
Y
。如何将
super
传递给该回调


除了使用箭头功能,还有其他解决方案吗?

一种可能的解决方案是使用箭头功能,例如

asyncMethod( err => super.method( err ) );

一种可能的解决方案是使用箭头函数,例如

asyncMethod( err => super.method( err ) );

我肯定会使用箭头函数,但是,如果您需要其他选择,请点击这里

根据JavaScript闭包行为,您可以将对
super.method
的引用存储在变量中,并在回调中使用它

代码如下:

class X extends Y
{
   constructor() {  super(); } 

   method() {
      let superMethod = super.method;

      asyncMethod(function (err) {   
          superMethod(err);
      });
   }
}

我肯定会使用箭头函数,但是,如果您需要其他选择,请点击这里

根据JavaScript闭包行为,您可以将对
super.method
的引用存储在变量中,并在回调中使用它

代码如下:

class X extends Y
{
   constructor() {  super(); } 

   method() {
      let superMethod = super.method;

      asyncMethod(function (err) {   
          superMethod(err);
      });
   }
}

请注意,根据
asyncMethod
的实现,此代码的行为可能不同(因为它返回)。请注意,根据
asyncMethod
的实现,此代码的行为可能不同(因为它返回)。