1、形参设置默认值
/* function sum(num1,num2){
num1 = num1 || 10;
num2 = num1 || 10;
console.log(num1+num2);
} */
function sum(num1=10,num2=10){
console.log(num1+num2);
}
sum(20,30);
2、延展操作符
function sum(name, sex, ...nums){
let result = 0;
console.log(name);
console.log(sex);
for(let value of nums){
value += value;
}
return result;
}
console.log('bob','男',10,20,30);//bob 男 60
3、箭头函数
let sum = (num1,num2) => {return num1 + num2;}
//箭头函数不用担心this指向问题
function demo(){
setTimeout(function(){
console.log(this);//指向window
},3000);
setTimeout(() => {
console.log(this);//指向{}
},3000);
}
let obj = {};
demo.call(obj);
网友评论