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

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

你好,我放弃了。任何人帮助我或有另一种方式来调用该函数?

4

4 回答 4

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

希望这有帮助。

于 2012-07-17T06:53:01.283 回答
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/

于 2012-07-17T06:51:13.843 回答
1

不修改就无法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()

于 2012-07-17T07:08:43.123 回答
0
var stringWord;
function firstFunction()
{
  stringWord = "Hello World";
  secondFunction();
} 

 function secondFunction()
  {
    alert(stringWord);
  }

function thirdFunction()
{
  run = setTimeout( 'secondFunction()' , 5000);
}
于 2012-07-17T06:39:12.053 回答