如何调用内部 JavaScript 类方法



我是 JavaScript 中的 OOP 新手。有人可以指出我将如何从类中调用内部函数。

例如,从下面的代码中,我将如何使用myFunction调用hello函数:

// app.js file
var Api = require('Api')
var api = new Api();
api.myFunction();
//server.js file
/**
 * API client.
 */
function Api() {
    this.my_var = 'my variable';
}
/**
 * My Function
 */
Api.prototype.myFunction = function() {
    // have tried this
    this.hello();
    // and this
    Api.hello();
}
/**
 * Hello
 */
Api.prototype.hello = function() {
    console.log('Hello!');
}
// expose the Api class
module.exports = Api;
module.exports = function() {
    this.my_var = 'my_variable';
    this.myFunction = function() {
        this.hello();
    };
    this.hello = function() {
        console.log('hello');
    };
    return this;
}

最新更新