博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ES6阅读笔记
阅读量:4552 次
发布时间:2019-06-08

本文共 2414 字,大约阅读时间需要 8 分钟。

一.babel转换器:babel.js.io;

 

二.变量声明.

1.块级作用域let:

传统var声明: var a = [];for (var i = 0; i < 10; i++) {  a[i] = function () {    console.log(i);  };}a[6](); // 10 变量let声明:
var a = [];for (let i = 0; i < 10; i++) { a[i] = function () { console.log(i); }; } a[6](); // 6
 

2.常量const:一旦声明不可改变,一般用来引入模块,例如:const moment=require("moment");

 

三.类Class

class Animal {    constructor(){        this.type = 'animal'    }    says(say){        console.log(this.type + ' says ' + say)    }}let animal = new Animal()animal.says('hello') //animal says helloclass Cat extends Animal {    constructor(){        super()//指代父类的实例,必须要有。        this.type = 'cat'    }}let cat = new Cat()cat.says('hello') //cat says hello

 

四.箭头函数arrow function

1.简化写法

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

2.使用箭头函数时,函数体内的this对象,就是定义时所在的对象,看例题:

class Animal {    constructor(){        this.type = 'animal'    }    says(say){        setTimeout(function(){            console.log(this.type + ' says ' + say)        }, 1000)    }} var animal = new Animal() animal.says('hi')  //undefined says hi,因为setTimeout中的this对象指向全局对象,在ES5中一般是把this赋值给常量然后代入,或者使用.bind(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

 

五.template string:简化拼接字符串写法

$("#result").append(  "There are " + basket.count + " " +  "items in your basket, " +  "" + basket.onSale +  " are on sale!");//es5中,使用拼接字符串形式$("#result").append(`  There are ${basket.count} items   in your basket, ${basket.onSale}  are on sale!`);//es6中,将字符串放在单引号内,变量则放在${}中

六.解构:destructuring:允许按照一定模式,从数组和变量中取得值,对变量进行赋值.

let cat = 'ken'let dog = 'lili'let zoo = {cat: cat, dog: dog}console.log(zoo)  //Object {cat: "ken", dog: "lili"},es5let cat = 'ken'let dog = 'lili'let zoo = {cat, dog}console.log(zoo)  //Object {cat: "ken", dog: "lili"},es6 反过来:
let dog = {
type: 'animal', many: 2} let { type, many} = dog console.log(type, many) //animal 2
 

七.default默认值

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

rest

function animals(...types){    console.log(types)}animals('cat', 'dog', 'fish') //["cat", "dog", "fish"]

 

转载于:https://www.cnblogs.com/YutaoZhou/p/6658002.html

你可能感兴趣的文章
make_head,,,pop_head,,,push_head,,,sort_head..
查看>>
c语言数据问题
查看>>
编程之美2015资格赛 解题报告
查看>>
团队开发
查看>>
异步加载JS的方法。
查看>>
golang-gorm框架支持mysql json类型
查看>>
【tool】白盒测试
查看>>
图论其一:图的存储
查看>>
20180923-WebService
查看>>
z变换
查看>>
Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数
查看>>
Android群英传神兵利器读书笔记——第二章:版本控制神器——Git
查看>>
第十二周作业
查看>>
poj 1008 模拟题
查看>>
Spring基础2
查看>>
HTML页面转换为Sharepoint母版页(实战)
查看>>
lua 实现公式配置
查看>>
MySQL在windows上导入数据提示错误(Errcode: 13 - Permission denied)
查看>>
什么叫活着!
查看>>
【灵异短篇】这个夜晚有点凉
查看>>