如何将函数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>
答案 0 :(得分:3)
将结果传递给numTwo
:
function numOne(x, y){
var result = x+y;
numTwo(result);
}
function numTwo(r){
alert(r);
}
numOne(2, 3);
答案 1 :(得分: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);
}