如何从另一个函数调用一个数字到一个函数中?
问题描述:
如何将函数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>
答
结果传递给numTwo
:
function numOne(x, y){
var result = x+y;
numTwo(result);
}
function numTwo(r){
alert(r);
}
numOne(2, 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);
}
你可以把它全球性的,还是手动传递给'numTwo':'numTwo(X);' – dashtinejad 2014-10-18 07:41:01
我怎样才能让全球? – Den 2014-10-18 07:41:49
通过将var x = 3拉出函数来使其成为全局函数 – 2014-10-18 07:43:16