Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何调用内部javascript类方法?_Javascript_Node.js - Fatal编程技术网

如何调用内部javascript类方法?

如何调用内部javascript类方法?,javascript,node.js,Javascript,Node.js,我不熟悉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';

我不熟悉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;

如果您先设置Api.prototype.hello,然后设置Api.prototype.myFunctionyeah!尝试将hello()函数定义置于myFunction()定义之上。您的代码运行良好,this.hello应该可以运行。你能在不起作用的地方展示更多的代码吗<代码>var api=新api;api.myFunction()有效。@vs_lala仅使用var定义的函数遵循顺序,在调用它们的行之后声明的函数仍然有效。正如@DaveChen所提到的,正在工作。。
module.exports = function() {

    this.my_var = 'my_variable';

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

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

    return this;
}