我已經在線查看有關函數中變化的變量,但仍然無效。更改函數中的全局變量JS
我想改變函數中的全局變量。一個有數字的對象相互相乘。該函數將使用幾個不同的全局變量。所以不要直接使用那裏的名字。
這裏是代碼
var homeTeamOppw = {"last_gp": 0.2, "2nd_gp": 0.2, "3rd_gp": 0.8, "4th_gp": 0.4, "5th_gp": 0.8};
ans = 0;
function timesArrayitems(teamOpp, array){
var num = 1;
for(item in array){
num = num * array[item];
}
teamOpp = num;
}
timesArrayitems(ans, homeTeamOppW);
console.log(ans);
俺們出來作爲取消定義。有人可以解釋請和謝謝
http://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference – mplungjan
你實際上並沒有'timesArrayitems更新'ans'()',你傳遞它作爲參考,它被用作'function'內的'local var teamOpp'作用域。 –