ES6/ES2015 核心内容使用

2016年,

最常用的es6特性

  1. let,const
  2. class,extent,super
  3. arrow function
  4. template string
  5. destructuring
  6. default rest

1.let,const

这两个的用途与var类似,都是用来声明变量的,但在实际运用中他俩都有各自的特殊用途。

1.1 let

var name = 'outer'
if (true) {
    var name = 'inner'
    console.log(name)  //inner
}
console.log(name)  //inner

js在browser中只有window(全局)和function有自己的作用域,和其他语言不同,没有块级作用域。
内层变量会覆盖外层同名变量。而let就是新增块级作用域。它所声明的变量只在代码块中有效。

let name = 'outer'
if (true) {
    let name = 'inner'
    console.log(name)  //inner
}
console.log(name)  //outer

tips*

1.let在块级作用域中存在变量提升
2.let具有块级作用域
3.不能在同一作用域中(函数作用域和块级作用域)用let声明相同的变量


另一个不合理的就是for循环中循环变量问题泄露为全局变量

var a = [];
for (var i = 0; i < 10; i++) {
  a[i] = function () {
    console.log(i);
  };
}
a[6](); // 10

i作为全局变量,在for循环时候i每次都被覆盖,这样每次输出的都是最后1轮的i值,而使用let则完美解决这个问题

var a = [];
for (let i = 0; i < 10; i++) {
  a[i] = function () {
    console.log(i);
  };
}
a[6](); // 6

之前的解决方式其实这个问题可以用闭包来进行解决,这样为每个i加上作用域。

var a = [];
for (var i = 0; i < 10; i++) {
  a[i] = iterator(i)
}
a[6](); // 6
function iterator(i){
    return function(){
        console.log(i)
    }
}

1.2 const


用来声明变量,但是声明是常量,一旦声明,便不可改变。
可以用来声明用来引用第3方库

const moment = require("moment");

2.class,extents,super

用来解决:原型、构造函数、继承、、
class Animal {
    constructor(){
        this.type = 'animal'
    }
    says(say){
        console.log(this.type + ' says ' + say)
    }
}

let animal = new Animal()
animal.says('hello') //animal says hello

class Cat extends Animal {
    constructor(){
        super()
        this.type = 'cat'
    }
}

let cat = new Cat()
cat.says('hello') //cat says hello

上面代码首先用class定义了一个“类”,可以看到里面有一个constructor方法,这就是构造方法,而this关键字则代表实例对象。
简单地说,constructor内定义的方法和属性是实例对象自己的,而constructor外定义的方法和属性则是所有实例对象可以共享的。

Class之间可以通过extends关键字实现继承,这比ES5的通过修改原型链实现继承,要清晰和方便很多。上面定义了一个Cat类,
该类通过extends关键字,继承了Animal类的所有属性和方法。

super关键字,它指代父类的实例(即父类的this对象)。子类必须在constructor方法中调用super方法,否则新建实例时会报错
。这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工。如果不调用super方法,子类就得不到this对象。
ES6的继承机制,实质是先创造父类的实例对象this(所以必须先调用super方法),然后再用子类的构造函数修改this。

3.arrow function

1.用来写function比原来的语法要简单的多.

//es5
function(x, y) { 
    x++;
    y--;
    return x + y;
}
//es6
(x, y) => {x++; y--; return x+y}

2.解决this指向问题

class Animal {
    constructor(){
        this.type = 'animal'
    }
    says(say){
        setTimeout(function(){
            console.log(this.type + ' says ' + say)//此时this指向是window
        }, 1000)
    }
}

 var animal = new Animal()
 animal.says('hi')

2.1传统解决问题的方法

says(say){
    var _this = this;//缓存this,替换this指向。
    setTimeout(function(){
        console.log(_this.type + ' says ' + say)//此时this指向是window

2.bind(this)

says(say){
    setTimeout(function(){
        console.log(this.type + ' says ' + say)
    }.bind(this), 1000)

now: we only need is this:

class Animal {
    constructor(){
        this.type = 'animal'
    }
    says(say){
        setTimeout( () => {
            console.log(this.type + ' says ' + say)
        }, 1000)
    }
}
 var animal = new Animal()
 animal.says('hi')  //animal says hi

当我们使用能够箭头函数的时候,函数体内的this就是定义对象是所在的对象,而不是使用时所在的对象。
此时箭头函数内部并没有绑定this的机制,实际原因是this就是继承自外部代码块中this。

4.template string

在我们拼接html到DOM中时,穿透的写法非常麻烦。通常我们会使用模版工具库,如mustache等。
old time

    $("#result").append("There are <b>" + basket.count + "</b>");

now

    $("#result").append(`There are <b>${basket.count}</b>`);

5.destructuring

es6 允许按照一定模式,可以从数组和对象里提取值,对变量进行赋值。

old time

let cat = 'ken'
let dog = 'lili'
let zoo = {cat: cat, dog: dog}
console.log(

now

let cat = 'ken'
let dog = 'lili'
let zoo = {cat, dog}
console.log(zoo)  //Object {cat: "ken", dog: "lili"}

or

let dog = {type: 'animal', many: 2}
let { type, many} = dog
console.log(type, many)   //animal 2

6.default ,rest

default 就是传入默认值

old time

function animal(type){
    type = type || 'cat'  
    console.log(type)
}
animal()

这样我们忘传默认值,也不会报错。
now

function animal(type = 'cat'){
    console.log(type)
}
animal()

rest 就是用来替换arguments

function animals(...types){
    console.log(types)
}
animals('cat', 'dog', 'fish') //["cat", "dog", "fish"]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,742评论 18 399
  • class的基本用法 概述 JavaScript语言的传统方法是通过构造函数,定义并生成新对象。下面是一个例子: ...
    呼呼哥阅读 4,123评论 3 11
  • ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准。因为当前版本的ES6是在2015...
    何wife阅读 388评论 0 0
  • [TOC] 参考阮一峰的ECMAScript 6 入门参考深入浅出ES6 let和const let和const都...
    郭子web阅读 1,807评论 0 1
  • 文/艺莫 也许你不曾记得 我们上辈子的那段情缘 也许你早已忘记了你我的誓言 亲爱的今生既无缘 来生定相伴 可我却把...
    艺莫阅读 269评论 13 21