這個問題在這里已經有了答案: 將區域變數從一個函式傳遞到另一個函式 3 個回答 3 小時前關閉。
如何從另一個函式訪問變數?函式 1 中的警報取函式 2 中的 2 個變數的總和
fun1();
function fun1() {
alert("var two var three = " two three);
}
function fun2() {
var one = 1;
var two = 2;
var three = 3;
var four = 4;
}
uj5u.com熱心網友回復:
要么在兩個函式之外全域定義這些變數(對于任何現實世界的程式來說都是非常糟糕的做法 - 但對于示例來說已經足夠了) - 或者將它們作為引數提供給警報函式。
var one, two, three, four;
fun1();
function fun1 ()
{
fun2();
alert("var two var three = " two three);
}
function fun2 ()
{
one = 1;
two = 2;
three = 3;
four = 4;
}
fun2();
function fun1 (one, two, three, four)
{
alert("var two var three = " two three);
}
function fun2 ()
{
fun1(1, 2, 3, 4);
}
uj5u.com熱心網友回復:
這不可能從Scope外部訪問變數
如果您想從更多功能訪問它,則將其設為全域。像下面。
fun2();
fun1();
var two = 2;
var three = 3;
function fun1() {
alert("var two var three = " two three);
}
function fun2() {
var one = 1;
two = two 2;
three = three 3;
var four = 4;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/355935.html
標籤:javascript
上一篇:嵌套物件陣列的打字稿介面
下一篇:單擊卡片內的特定元素
