我是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;
先谢谢。
答案 0 :(得分:0)
module.exports = function() {
this.my_var = 'my_variable';
this.myFunction = function() {
this.hello();
};
this.hello = function() {
console.log('hello');
};
return this;
}