- ES6中的默认参数
把这些默认值直接放在函数签名中
let link = function (height=50,color="red",width="10") {
console.log(height+color+width);
};
link();
调动函数式,参数不可以交换位置.
- ES6中的模板表达式
在反引号包裹的字符串中,使用${NAME}语法来表示模板字符:
let person = function (name="json",age=20,sex="man") {
let intro = `My name is ${name},I am ${age},I am a ${sex}.`;
console.log(intro);//My name is jackson,I am 16,I am a man.
};
person(name="jackson",age=16);
为解决排版问题而诞生
- 箭头函数
let add = (a=1,b=2)=>a+b;
console.log(add());
this的指向
//ES5
let obj = {
birth:1990,
getAge:function () {
let b=this.birth;
let fn = function () {
console.log("fn",this.birth);//this指向的是window,window中没有birth,所以是undefined
};
console.log(b);//1990
return fn();
}
};
obj.getAge()
ECMAScript6中的箭头函数则修复了this的指向问题
let obj = {
birth:1999,
getAge:function () {
let b = this.birth;
let fn = ()=>console.log("fn",this.birth);//1990,箭头函数修复this的指向,指向外部调用者obj
console.log(b);//1990
return fn();
}
};
obj.getAge();
所以说,之所以出现箭头函数的原因是为了解决this指向问题
- Set
里面的数据都是独一无二的
let set= new Set();
set.add(1);
set.add(2);
set.add(1);
set.add(3);
console.log(set); //Set { 1, 2, 3 }
//判断set中是否有b
console.log(set.has("b"));//true
- rest语法
let animal = (...types)=>{console.log(types)};
animal("cat","dog","fish");