2014-10-18 200 views
0

如何将函数numOne中的“x”调用到函数numTwo中?有没有更简单的方法来做到这一点?或者我怎样才能从numOne中调用numTwo的结果?如何从另一个函数调用一个数字到一个函数中?

<!doctype HTML> 
 
<html> 
 
    <head> 
 
     <title>Function Testing</title> 
 
    </head> 
 
    <body> 
 
     <script type="text/javascript"> 
 
      function numOne(x, y){ 
 
       var x = 3; 
 
       var y = 4; 
 
       var result = x+y; 
 
       numTwo(); 
 
      } 
 
      function numTwo(){ 
 
       alert(x); 
 
      } 
 
     </script> 
 
    </body> 
 
</html>

+3

你可以把它全球性的,还是手动传递给'numTwo':'numTwo(X);' – dashtinejad 2014-10-18 07:41:01

+0

我怎样才能让全球? – Den 2014-10-18 07:41:49

+1

通过将var x = 3拉出函数来使其成为全局函数 – 2014-10-18 07:43:16

回答

2

结果传递给numTwo

function numOne(x, y){ 
 
    var result = x+y; 
 
    numTwo(result); 
 
} 
 

 
function numTwo(r){ 
 
    alert(r); 
 
} 
 

 
numOne(2, 3);

3

你可以用全球价值做

var x; 
function numOne(x, y){ 
    x=3; 
    var y = 4; 
    var result = x+y; 
    numTwo(); 
} 
function numTwo(){ 
    alert(x); 
} 

或添加参数numTwo功能

function numOne(x, y){ 
    var x = 3; 
    var y = 4; 
    var result = x+y; 
    numTwo(x); 
} 
function numTwo(t){ 
    alert(t); 
}