假设我有两个函数function A(str)
和function B(id)
:
function A(str) {
}
function B(id) {
$x = str
}
有没有办法将函数A
中的参数str
传递给函数B
?
最佳答案
分配给 A 和 B 范围内的变量
let outerStr;
function A(str) {
outerStr = str;
}
function B(id) {
const $x = outerStr;
}
或者使用str
参数调用B:
function A(str) {
B(null, str);
}
function B(id, strParam) {
const $x = strParam;
}
(或使用this
或arguments
- 不推荐,但可行)
关于javascript - 将函数参数传递给另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49606486/