JS function sets the default parameter values

1, for ES6 before, be implemented in other ways within the function

For example:
The following example Transfer: https: //www.cnblogs.com/sntetwt/p/4174224.html

function Example (a, B) {
     var a = arguments [0] arguments [0]:. 1;? // set the default value of the parameter a. 1 
    var B = arguments [. 1] arguments [. 1]: 2;? // the default setting is the parameter b 2 
    return a + b;
}
function example(name,age){
    name = name || 'Diao' ;
    age=age||21;
    Alert ( '! Hello I am a' + name + ', this year' + age + 'years old.' );
}
function example(name,age){
    if(!name){name='貂蝉';}
    if(!age){age=21;}
    Alert ( '! Hello I am a' + name + ', this year' + age + 'years old.' );
}
function example(setting){
    var defaultSetting={
        name: 'Diao' ,
        age:'30',
        Sex: 'female' ,
        phone:'13611876347',
        QQ:'10086',
        birthday:'1949.10.01'
    };
    $.extend(defaultSetting,settings);
    var the Message = 'Name:' + defaultSetting.name
     + ', gender:' + defaultSetting.sex
     + ', Age:' + defaultSetting.age
     + ', phone:' + defaultSetting.phone
     + ', QQ:' + defaultSetting. QQ
     + ', birthday:' + defaultSetting.birthday
     + '. ' ;
    alert(message);
}
example({
    name: 'Zhaojun' ,
    Sex: 'female' ,
    phone:'10089'
});

2, ES6 can be used directly as a default parameters like other programming languages

function myFunction(x = 1, y = 2, z = 3) {
 console.log(x, y, z); // Outputs "1 7 9"
 }
 myFunction(undefined,7,9);

 

Guess you like

Origin www.cnblogs.com/zhaogaojian/p/12143375.html