我想调用函数A到函数B。
我已经尝试从函数 B 调用函数 A
module.exports = function(req, res) {
return {
function_a: function() {
// do something here
},
function_b: function() {
// call function_a
const A = function_a(); // I want to be called something like this
// do something here
}
}
}
我希望函数 A 在函数 B 内部被调用
最佳答案
假设通过以下方式调用function_b
:
your_module().function_b();
然后您可以在function_b
内使用this
访问该对象。
this.function_a();
如果该方法与对象分离,那么您将失去该连接,因此可能值得重写该模块,以便您拥有一个不依赖于 this
值的引用。例如:
module.exports = function(req, res) {
const my_return_value = {
function_a: function() {
// do something here
},
function_b: function() {
const A = my_return_value.function_a();
}
};
return my_return_value;
}
关于javascript - 在对象内部的函数内部使用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58408312/