function firstFunction()
{
stringWord = "Hello World";
function secondFunction()//How to call this function in thirdfunction() ??
{
alert(stringWord);
}
}
function thirdFunction()
{
run = setTimeout( ... , 5000);
}
嗨,我放弃了。有人帮我或者用另一种方式来调用这个函数吗?
答案 0 :(得分:2)
试试这个:
function firstFunction()
{
stringWord = "Hello World";
this.secondFunction = function()//How to call this function in thirdfunction() ??
{
alert(stringWord);
}
}
var instand = new firstFunction();
function thirdFunction(){
run = setTimeout( 'instand.secondFunction()', 5000);
}
希望得到这个帮助。
答案 1 :(得分:1)
function firstFunction()
{
stringWord = "Hello World";
return function secondFunction()//How to call this function in thirdfunction() ??
{
alert(stringWord);
};
}
secondFunction = firstFunction();
function thirdFunction()
{
run = setTimeout( 'secondFunction()' , 5000);
}
JSFiddle:http://jsfiddle.net/DRfzc/
答案 2 :(得分:1)
如果不修改secondFunction()
,则无法从firstFunction()
外部呼叫firstFunction()
。如果这是可以接受的继续阅读......
方法1:修改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()
。
答案 3 :(得分:0)
var stringWord;
function firstFunction()
{
stringWord = "Hello World";
secondFunction();
}
function secondFunction()
{
alert(stringWord);
}
function thirdFunction()
{
run = setTimeout( 'secondFunction()' , 5000);
}