Don’t quite understand the meaning of this code? Ask God to explain
function sum(){
var sumg=0;
for(var i=0;i<arguments.length;i++){
sumg+=arguments[i];
}
return sumg;
}
alert(sum(1,2,3,4,5));
世界只因有你2017-05-19 10:25:23
arguments
就是参数的意思
这个函数是为了求和,所以参数的数量不是固定的,可能是 sum(1,2)
,可能是 sum(1,2,3)
Since the parameters are not fixed
then we need to get the parameters
Look at the following code
for(var i=0;i<arguments.length;i++){
sumg+=arguments[i];
}
For example, we enter sum(1,2)
sum(1,2)
这时 arguments.length=2
arguments[i]
的值分别是1
和2
At this time, the values of arguments.length=2
arguments[i]
are 1
and 2
This achieves the purpose of passing parameters🎜
PHPz2017-05-19 10:25:23
arguments is a built-in attribute of the current function. It is an array-like array that stores the actual parameters of the incoming function. The length is the number of incoming actual parameters. The meaning of this code is to find 1+2+3+4+ Value of 5