javascript - What does arguments.length mean in for loop?
ringa_lee
ringa_lee 2017-05-19 10:23:23
0
2
500

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));
ringa_lee
ringa_lee

ringa_lee

reply all(2)
世界只因有你

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] 的值分别是12At this time, the values ​​of arguments.length=2

arguments[i] are 1 and 2

This achieves the purpose of passing parameters🎜
PHPzhong

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template