2014-01-06 47 views
0

我想要如何调用函数中的函数。如何在JavaScript中调用函数中的函数

这是我的函数:

function TempConvert() { 
     var tempVal = prompt('Enter a temperature'); 
     var newTemp; 

     newTemp = (tempVal - 32/1.8 + 273.15); 
     alert(newTemp); 
     } 

这需要它被称为:

function iMenu() { 
    var choice = prompt("Type in a number for the corresponding option\nOption 1: Temperature Converter\nOption 2: Option 2\nOption 3: Exit"); 
    if (choice == "1") { 

     // TempConverter needs to go here <<---- 

    } 
     else if (choice == "2") { 
     alert('Option2'); 
    } 
     else { 
     alert('Bye'); 
     return; 
    } 
} 

感谢。我试过把它放在那里,但它没有效果。

+3

http://eloquentjavascript.net/chapter3.html –

+0

你能说明这些函数是如何位于整个代码中的吗?看起来像某种范围的问题,所以我们需要知道什么是什么功能。 – kmoe

回答

0

所有你需要做的是调用函数,就像这样:

if (choice == "1") { 
    TempConvert(); 
} 
0
TempConvert(); 

是你所需要的。函数范围似乎在范围内,那么你的问题是什么?

从您的问题的标题看,看起来您不能调用函数外的函数,因为范围在之内。您只能调用功能全局的函数。

只有我能想到这样做的一种方法:

移动功能的阵列,像myArray[0] = (TempConvert()),只要你想使用它,只是执行myArray[0];

+0

也许'TempCovert'是本地的一个不同的功能? –

0

你可以叫喜欢,

function iMenu() { 
    var choice = prompt("Type in a number for the corresponding option\nOption 1: Temperature Converter\nOption 2: Option 2\nOption 3: Exit"); 
    if (choice == "1") { 
     TempConvert(); // added here 
    } else if (choice == "2") { 
     alert('Option2'); 
    } else { 
     alert('Bye'); 
     return; 
    } 
} 
0

只需拨打你的函数()

function iMenu() { 
     var choice = prompt("Type in a number for the corresponding option\nOption 1: Temperature Converter\nOption 2: Option 2\nOption 3: Exit"); 
     if (choice == "1") { 

      TempConvert(); 

     } 
      else if (choice == "2") { 
      alert('Option2'); 
     } 
      else { 
      alert('Bye'); 
      return; 
     } 
    } 
0

这里没有关于范围的问题。如果两个功能处于同一水平,请致电TempConvert();可能是您将其拼写错误。