我如何在javascript中创建类后运行函数

Dmi*_*nov 1 javascript node.js

在创建类和使用类方法之后,我只想在onсe上调用构造函数.我如何在JavaScript中执行此操作?谢谢!

var Test=function(){
        //hello(); // why not working? How call?
}

Test.prototype.hello=function(){
        console.log(1);
}

var t=new Test();
t.hello()
Run Code Online (Sandbox Code Playgroud)

https://jsfiddle.net/dmitriykupriynov/doo6bj0b/

mad*_*ox2 7

在构造函数中,您可以通过this关键字访问对象的方法:

var Test=function(){
    this.hello();
}
Run Code Online (Sandbox Code Playgroud)