Function
# Function
# 参数默认值
// 传 undefined 取默认赋值
function f(x = 1,y = 2,z){
return x + y + z;
}
f(undefined,4,5) // 10
// 默认值 可以是其他参数值
function f(x = 1,y = 2,z = x + y){
return x * 10 + z;
}
f(1,undefined) // 13
// f.length 获取没有设置默认参数的参数数量
function f(x ,y ,z = x + y){
return x * 10 + z;
}
console.log(f.length) // 2
// es6不推荐使用
# 获取可执行参数
# es5获取参数
// es5 arguments 可以获取参数
function f(){
// arguments 是伪数组
var num = 0;
Array.prototype.forEach.call(arguments,function(item){
num += item*1
})
return num
}
f(1,2,3) // 6
# es6获取参数
// es6 ...[arg] 可以获取参数
function f(...nums){
// nums 是数组
var num = 0;
nums.forEach(item =>{
num += item*1
})
return num
}
f(1,2,3) // 6
# es6箭头函数
let f = function (){}
let f = ()=>{}