javascript,function的传参问题?
vartheArray=[1,2,3];varnewtotals=[9,12,21];functionadd_to_total(totals,x){totals=newt...
var theArray=[1,2,3];
var newtotals=[9,12,21];
function add_to_total(totals,x)
{
totals=newtotals;
document.write("Function内totals是:"+totals);
}
add_to_total(theArray,2);
document.write("<br>");
document.write("Function外部的totals,也就是theArray是:"+theArray);
把上面的add_to_total换成,下面的:
function add_to_totals(totals, x)
{
totals[0] = totals[0] + x;
totals[1] = totals[1] + x;
totals[2] = totals[2] + x;
}
就改变了,为什么?
ps:“改变”指的是:“theArray的值”。 展开
var newtotals=[9,12,21];
function add_to_total(totals,x)
{
totals=newtotals;
document.write("Function内totals是:"+totals);
}
add_to_total(theArray,2);
document.write("<br>");
document.write("Function外部的totals,也就是theArray是:"+theArray);
把上面的add_to_total换成,下面的:
function add_to_totals(totals, x)
{
totals[0] = totals[0] + x;
totals[1] = totals[1] + x;
totals[2] = totals[2] + x;
}
就改变了,为什么?
ps:“改变”指的是:“theArray的值”。 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询