常用ES6特性

  • 用let 来代替var即可,let定义不具有变量提升作用,同一作用域内不可重复定义,块级作用域

  • const表示常量初始化后不可被修改,初始化必须被赋值,一旦初始化之后不可被改变

  • class 和 extends

class base {    
  constructor(name) {      
  this.name = name;   
 };   
 say() {        
  console.log(this.name);   
 }
}
class thread extends base {   
 constructor(name, age) {      
  super(name);   
     this.age = age;  
  }   
 say() {  
      console.log(this.name, this.age);    
  }
}
  • 字符串模板
console.log(`abcd${this.e}`)
  • 默认参数
function say(name='Lyf',age=22){
console.log(name,age);
}
say('mx');
  • 箭头函数,定义时就绑定不会被上下文环境改变,其值取决于父环境中this
o={
name:'name',
out1:function(){console.log(this.name)},
out2:()=>{console.log(this.name)}
}
o.out1()//name
o.out2()//undefined
(function () {
    name = 'quanjubianliang';
    var o = {
        name: 'name',
        out1: function () {
            console.log(this.name)
        },
        out2: ()=> {
            console.log(this.name)
        }
    };
    o.out1();//quanjubianliang
    o.out2();//name
})()
var oo = function () {
    this.name = 'name';
    this.out1 = function () {
        console.log(this.name)
    };
    this.out2 = ()=> {
        console.log(this.name)
    }
};
new oo().out1.call({name:'test'})//test
new oo().out2.call({name:'test'})//name
  • 解构
var [x,y,z]=[1, 2];
var person = {firstName: "John", lastName: "Doe"};
var {firstName, lastName} = person;
console.log(firstName,lastName);

你可能感兴趣的:(常用ES6特性)