在对象内部的函数内部使用函数
我想将函数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
}
}
}
回答如下:假设function_b
通过以下方式调用:
your_module().function_b();
然后您可以通过this
中的function_b
访问对象。
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;
}
在对象内部的函数内部使用函数
我想将函数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
}
}
}
回答如下:假设function_b
通过以下方式调用:
your_module().function_b();
然后您可以通过this
中的function_b
访问对象。
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;
}
与本文相关的文章
评论列表 (0)
- 暂无评论