Javascript 从实例方法调用实例方法

Javascript 从实例方法调用实例方法,javascript,node.js,methods,mongoose,Javascript,Node.js,Methods,Mongoose,我想从method2中调用method1。不知道如何访问它。我收到: TypeError:无法调用未定义的方法“method1” 不要使用箭头功能。它使用函数定义的词法范围中的这个上下文。在您使用的严格模式下,它是未定义的 使用常规功能: TestSchema.methods = { method1: function() { return true; }, method2: function() { if(this.method1()){ con

我想从method2中调用method1。不知道如何访问它。我收到:

TypeError:无法调用未定义的方法“method1”


不要使用箭头功能。它使用函数定义的词法范围中的
这个
上下文。在您使用的
严格模式下,它是未定义的

使用常规功能:

TestSchema.methods = {
  method1: function() {
      return true;
  },
  method2: function() {
    if(this.method1()){
        console.log('It works!');
    }
  }
};
然后确保将函数作为对象上的方法调用:

TestSchema.methods.method2();

您可以找到更多关于arrow函数作为方法的解释。

此错误的发生仅仅是因为arrow函数。箭头函数表达式不绑定自己的
this
参数
super
new.target

此外,您不应该使用
function
关键字来解决此问题。最好的解决办法是使用


如何调用
method2
?Test Test=new Test();试验方法2()@70656e6973什么是
Test
?@DmitriPavlutin TestSchema是“Test”对象的模型。到目前为止,为了跟上ES6的步伐。感谢您的时间,感谢您提供的进一步解释+其他解决方法!
TestSchema.methods.method2();
TestSchema.methods = {
  method1(){
    return true;
  },
  method2(){
    if (this.method1()) {
      console.log('It works!');
    }
  }
};
TestSchema.methods.method2();