JavaScript函数参数设置

在ES6中允许参数赋初始值

function add(a,b,c=3){
    console.log(a, b, c);
}
add(1,2)

ES6中参数解构

function connection({host='127.0.0.1',username,password,port}){
            
}
connection({
    host:'192.168.1.1',
    username:'root',
    password:'123123',
    port:3306
})

ES6引入了rest参数,用于替换argements

function date(){
    console.log(arguments)//arguments是一个类数组对象,包含函数调用时传递的所有参数
}
date('小沈阳','赵四','刘能','宋小宝')
//输出结果:Arguments(4) ["小沈阳", "赵四", "刘能", "宋小宝"]
//ES6,注意...args写在最后
function date(a,b,...args){
    console.log(a)
    console.log(b)
    console.log(args)
}
date('小沈阳','赵四','刘能','宋小宝')//...args是ES6引入的一个特性,用于捕获函数调用时超出前两个参数的所有参数

你可能感兴趣的:(javascript,前端,开发语言)