停用var 换用let const
let a = 20;
const b = 30;
类似php的直接引用值
let a="hello",c="world",op="tryES6!!";
const string = `${a}+${c}:::${op}`;
console.log(string);//hello+world:::tryES6!!
箭头函数
const fn = (a,b) => (a*60+30+b);
fn(3,5);//215
const foo = ()=>{
const a=30;
const b=60;
return a+b;
}
foo();//90
const fx = () => {
console.log("Hello ES6!");
}
fx();//Hello ES6!
两种主要用法
- 直接返回值
- 声明函数
默认值
function add(x=20,y=30){
return x+y;
}
add();
解析结构
const props = {
size : 1,
src : 'xxxx',
mode : 'die'
}
const {size, src} = props;
console.log(size);//1
其实就是利用了一个结构来快速获取对象中的某个值,这样可以大量简写
展开运算符
const arr1 = [1,2,3];
const arr2 = [...arr1,4,5,6];//[1,2,3,4,5,6]
const obj1={a:10, b:20, c:30};
const obj2={...obj1, d:50, e:60};//{a: 10, b: 20, c: 30, d: 50, e: 60}
进阶:
const props = {
size: 1,
src: 'xxxx',
mode: 'si'
}
const { size, ...others } = props;
console.log(others)
<button {...others} size={size}/>
利用展开运算符将可能出现的其他不定参数取出
Class!
class Person{
constructor(name, age){
this.name=name;
this.age=age;
}
getName(){ return this.name;5}
}