Javascript study notes function

The function used for function definition in JavaScript:

// foo(a[, b], c)
// 接收2~3个参数,b是可选参数,如果只传2个参数,b默认为null:
function foo(a, b, c) {
    if (arguments.length === 2) {
        // 实际拿到的参数是a和b,c为undefined
        c = b; // 把b赋给c
        b = null; // b变为默认值
    }
    // ...
}

1. The arguments keyword is to get the total number of parameters passed

2. The rest keyword is to obtain the redundant parameters of the passed parameters and generate an array (Es6, but I tried rest and can be changed to other names... ╮(╯▽╰)╭)

function foo(x,...g) {
    console.log('x = ' + x); // 10
    console.log(g)
    alert(g)
}
foo(10,13,31);

 Three parameters must be added in front of it. Only effective

3. Construct assignment (Es6)

// If the browser supports destructuring assignment, no error will be reported:
 var [x , y , z] = [ 'hello' , 'JavaScript' , 'ES6' ] ;//Of course, the latter array can also be replaced with the object var person {name:"bob",age:"21"}
 alert (x+ "" +y+ "" +z) ;

4. Introduce new constant definitions and left (Es6)

The previous constant was represented in uppercase, var PI = 3.14 ; now the const keyword is added, and the value cannot be changed after the definition (my WS will report an error if the value is changed), but some browsers have no effect.

left defines local variables, which are mainly used in for loops. Changes cannot be called outside the for loop, otherwise an error will be reported.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325770019&siteId=291194637