如何调用内部的javascript类方法?

时间:2016-02-07 03:44:31

标签: javascript node.js

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

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

// 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;

先谢谢。

1 个答案:

答案 0 :(得分:0)

module.exports = function() {

    this.my_var = 'my_variable';

    this.myFunction = function() {
        this.hello();
    };

    this.hello = function() {
        console.log('hello');
    };

    return this;
}
相关问题