方法中的JavaScript调用方法

时间:2018-06-25 09:35:08

标签: javascript oop

我正在尝试用OOP javascript创建一个简单的程序。当我尝试在method1中调用method2时,出现以下错误这是未定义的

class Hell()
{
   constructor()
   {
       alert("welcome");
   }

   method1()
   {
       alert("method1");
       this.method2();
   }

   method2()
   {
       alert("method2");
   }
}

这样行不通吗?

1 个答案:

答案 0 :(得分:0)

尝试一下:

class Hell{
  constructor()
  {
   alert("welcome");
  }

  method1()
  {
   alert("method1");
   this.method2();
  }

  method2()
  {
   alert("method2");
  }
}

var a = new Hell();
a.method1();