不修改就无法secondFunction()
从外部调用。如果这是可以接受的,请继续阅读...firstFunction()
firstFunction()
方法一:修改firstFunction()
返回引用secondFunction()
:
function firstFunction() {
stringWord = "Hello World";
return function secondFunction() {
alert(stringWord);
}
}
// And then call `firstFunction()` in order to use the function it returns:
function thirdFunction() {
run = setTimeout(firstFunction(), 5000);
}
// OR
var secondFunc = firstFunction();
function thirdFunction() {
run = setTimeout(secondFunc, 5000);
}
方法 2:已将firstFunction()
引用secondFunction()
放在其范围之外可访问的变量中:
var secondFunc;
function firstFunction() {
stringWord = "Hello World";
function secondFunction() {
alert(stringWord);
}
window.secondFunc = secondFunction; // to make a global reference, AND/OR
secondFunc = secondFunc; // to update a variable declared in same scope
// as firstFunction()
}
firstFunction();
function thirdFunction() {
run = setTimeout(secondFunc, 5000);
}
请注意,无论哪种方法,您都必须在尝试使用内部函数之前实际调用。 firstFunction()