ES6 的新特性

149 阅读3分钟

1.函数的默认参数

function Fun(a=1,b=2) {} // 不传 ab 参数的时候默认a=1b=2。传了参数以传的参数为准

function Fun(a, b) {     // ES5 定义默认参数的方法,但参数为 0 的时候,会有问题
    let a = a || 1
    let b = b || 2
}

2.模板对象(反引号 + ${})

let name = 'kobe'
let say1 = `My name is ${name}.` // 模板对象的使用例子

let say2 = 'My name is ' + name + '.' // ES5

3.解构赋值

const people = {
    name: 'kobe',
    age: '24',
    sex: '男'
}
const {name, sex} = people // 解构赋值 name => kobe, sex => '男' 

var name = people.name  // ES5
var sex = peopele.sex

const list = [1, 2, 3, 4, 5]
const [a, , b, c, d] = list  // a=>1, b=>3, c=>4, d=>5

4.增加的对象字面量

const name = 'kobe'
const age = 'age'

const obj = {
    name,  // ES6 在对象中,属性名和变量名相同的时候,可以省略冒号和后面的变量引用
    // ES5的写法: name: name,
    
    [age]: 18, // ES6 可以直接在对象内部书写动态的属性名
    
    message() { // 对象内部的函数定义方式也可进行简化
        console.log(this.name)
    }
    // message: function(){}  ES5    
}

obj[age] = 21 // age 动态改变 对象外部

// 可以通过 Object.create 方法继承对象
const obj1 = {name: 'kobe', age: '18'}
let obj2 = Object.create(obj1)
obj2 = {sex: 'men'} // 在 obj2 的 __proto__ 属性里面有 obj1 的属性

5.箭头函数。 this 指向器外层作用域的 this(指向定义时的this,非执行时的this)

// ES5
var logUpperCase = function() {
  var _this = this;
  
  this.string = this.string.toUpperCase();
  return function () {
    return console.log(_this.string);
  }
}
logUpperCase.call({ string'ES6 rocks' })();

// ES6 
var logUpperCase = function() {  
    this.string = this.string.toUpperCase();  
    return () => console.log(this.string);
}
logUpperCase.call({ string'ES6 rocks' })(); 

6.promise

var promise1 = new Promise(function(resolve){
    resolve(2);
});
promise1.then(function(value){
    return value * 2;
}).then(function(value){
    return value * 2;
}).then(function(value){
    console.log("1"+value);
});
// 使用方法链的then,使多个then方法连接在一起了,
// 因此函数会严格执行 resolve – then — then – then的顺序执行,
// 并且传递每个then方法的value的值都是前一个promise对象中return的值;
// 因此最后的结果就是18了;

7.块作用域和变量申明方式 let、const (均在当前块作用域内有效)

// let 申明的变量,只能在 let 命令所在的代码块内起作用

// const 申明一个只读的常量,一旦声明,常量的值就不能改变

// 当 const 声明一个 对象 的时候,对象的属性是可以改变的
const a = {num: '10'}
a.num = '12' // 这是可以的
// 因为对象是引用类型,const 定义的对象a中保存的是指向对象a的指针,修改对象属性不会让指向对象的指针发生变化。
// const声明的只是栈区内容不变,基本数据类型保存在栈区中不可改变;引用数据类型在栈区保存的地址不可改变。
// 所以const定义对象,对象的属性是可以改变的

// 1.可以使用Object.freeze(obj)冻结obj,就能使其内部的属性不可变,
// 但有局限,就是obj对象中要是有属性是对象,该对象内属性还能改变,
// 要全不可变的话,就需要使用递归等方式一层一层全部冻结。

// 2.对象常量,结合 writable:false 和 configurable: false 
// 就可以创建一个真正的常量属性(不可修改、重定义或者删除)
var myObject = {};
Object.defineProperty( myObject, "FAVORITE_NUMBER", {
	value: 23,
	writable: false,
	configurable: false
});

8.class 类

//定义类
class Point {

  //constructor 方法是类的默认方法,通过 new 命令生成对象实例时,自动调用该方法。
  //一个类必须有 constructor 方法,如果没有显式定义,一个空的 constructor 方法会被默认添加。
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  toString() {
    return '(' + this.x + ', ' + this.y + ')';
  }

}

var point = new Point(2, 3);

point.toString() // (2, 3)

point.hasOwnProperty('x') // true
point.hasOwnProperty('y') // true
point.hasOwnProperty('toString') // false
point.__proto__.hasOwnProperty('toString') // true

9.Modules (模块)

import {name} from 'my-module'