我有文件
tools.js
我有多个功能:
const another_module = require('another_module');
module.exports = {
js_call: function(args={}) {
/* This function is executed when a JavaScript function should be called
Call structure:
args = {
'object': 'object.name',
'function': 'method_name',
'params': ['arg1', 'arg2']
}
*/
var self = this;
var o = null;
if ('object' in args) {
if (args.object == 'tools') {
o = self;
}
if (args.object == 'another_module') {
o = another_module;
}
}
if ('function' in args) {
if ('params' in args) {
o[args['function']].apply(null, args['params']);
} else {
o[args['function']]();
}
}
},
loaded_function: function (params={}){
var self = this;
self.test_function(); // this does not work here >> self.test_function is not a function
},
test_function: function (){
console.log('TEST');
}
}
所以如果我在另一个模块中这样做:
const tools = require('tools');
params = {
'object': 'tools',
'function': 'loaded_function',
}
tools.js_call(params);
我得到错误
self.test_function is not a function
我想知道我是否正在失去
self
当我使用
o[args['function']]();
或与
o[args['function']].apply(null, args['params']);
.
我如何解决或解决这个问题?
注释
:我正在使用node.js 8.9.3