Javascript 如何在函数外部调用此函数?

Javascript 如何在函数外部调用此函数?,javascript,Javascript,嗨,我放弃了。有人帮我吗,或者有其他方法调用该函数吗 function firstFunction() { stringWord = "Hello World"; function secondFunction()//How to call this function in thirdfunction() ?? { alert(stringWord); } } function thirdFunction() { run = setTimeout( ... , 5

嗨,我放弃了。有人帮我吗,或者有其他方法调用该函数吗

function firstFunction()
{
  stringWord = "Hello World";
  function secondFunction()//How to call this function in thirdfunction() ??
  {
    alert(stringWord);
  }
} 

function thirdFunction()
{
  run = setTimeout( ... , 5000);
}
JSFiddle:

jsiddle:

试试这个:

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);
}
希望能有所帮助。

试试这个:

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);
}

希望有帮助。

如果不修改
firstFunction()
,就无法从外部调用
secondFunction()
。如果可以接受,请继续阅读

方法1:修改
firstFunction()
以返回对
secondFunction()
的引用:

方法2:让
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);
}   

请注意,无论使用哪种方法,在尝试使用内部函数之前都必须实际调用
firstFunction()

如果不修改
firstFunction()
,就无法从外部调用
secondFunction()
。如果可以接受,请继续阅读

方法1:修改
firstFunction()
以返回对
secondFunction()
的引用:

方法2:让
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);
}   

请注意,无论使用哪种方法,您都必须在尝试使用内部函数之前实际调用
firstFunction()

为什么不将第二个函数放在外部?我发现此链接很有用。您真正想做的是什么?我找到了这个链接。也许它对你有用。@hkulekci谢谢,它很有帮助。你为什么不把第二个函数带到外面去呢?我发现这个链接很有用。你到底想做什么?我找到了这个链接。也许它对你有用。@hkulekci谢谢,它很有帮助。嘿,这很有帮助。非常感谢。考虑把它标记为这个问题的正确答案,然后;嘿,这有帮助。非常感谢。考虑把它标记为这个问题的正确答案,然后;很好,运行得很好。谢谢你的帮助。太棒了,运行得很好。谢谢你的帮助。这很有帮助!!非常感谢你。当我尝试这个时,我感觉很好。这非常有帮助!!非常感谢你。当我尝试这个的时候,我感觉很好。
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);
}