title: es6 新特性
preview: imgs/preview/es6_1.jpg
preview_text: es6 新特性 主要包含“类、模块化、箭头函数、函数参数默认值、模板字符串、解构赋值、延展操做符、对象属性简写、Promise、let与const”
tags:
- javascript
---javascript
这三个特性涉及了ES5中最使人头疼的的几个部分:原型、构造函数,继承...ES6提供了更接近传统语言的写法,引入了Class(类)这个概念。新的class写法让对象原型的写法更加清晰、更像面向对象编程的语法,也更加通俗易懂。java
class Animal { constructor(name, color) { // 构造函数 this.name = name; this.color = color; } toString() { // toString方法 console.log(`name: ${this.name}, color: ${this.color}`); } getName() { // 取值 return this.name; } setName(value) { // 存值 this.name = value; } getColor() { // 取值 return this.color; } setColor(value) { // 存值 this.color = value; } } let animal = new Animal("cat", "white"); animal.toString(); // name: cat, color: white console.log(animal.hasOwnProperty('name')) // true console.log(animal.hasOwnProperty('toString')) // false console.log(animal.__proto__.hasOwnProperty('toString')) // true
class Cat extends Animal { constructor(action = "catch thing",name, color) { super("cat", "black"); // super用做函数, 必须在this使用前调用 this.action = action; // Cat 类自己属性 } toString() { super.toString();// super用做对象 } getAction() { return this.action; } setAction(value) { this.action = value; } } let cat = new Cat("eat fish"); cat.toString(); // name: cat, color: white console.log(cat instanceOf Cat) // true console.log(cat instanceOf Animal) // true
function Animal(name, color) { this.name = name || "cat"; this.color = color || "orange"; } Animal.prototype.toString = function() { console.log(`name: ${this.name}, color: ${this.color}`); } function Cat (action, name, color) { Animal.call(this, name, color); this.action = action || "catch thing"; } Cat.prototype = Object.create(Animal.prototype); Cat.prototype.constructor = Cat; Cat.prototype.toString = function() { Tree.prototype.toString.call(this); } Cat.prototype.setAction = function(value) { this.action = value; }
es5不支持原生的模块化,在es6模块做为重要的组成部分被添加进来。模块的共能主要由export和import组成。每个模块都有本身的单独的做用域,模块之间的调用是经过export来规定模块对外暴露的接口,经过import来引用其余模块提供的接口。同时模块还创造了命名空间,防止函数的命名冲突。es6
ES6容许在一个模块中使用export来导出多个变量和函数。编程
export let varible = "test variable exprot"; export default varible; let name = "cat"; let color = "yellow"; export {name, color}
export const NUMBER_LENGTH = 20
export function myModuleFn(somParams) { return somParams }
定义好模块的输出,就能够在另外一个模块经过import导入数组
import varible from "test.js" import {myModuleFn} from "myModuleFn"; import {name, color} from "./path/to/test.js";
=>
不仅是function
的简写,它还带来了其余好处。箭头函数与包围它的代码共享同一个this,可以帮你解决this的指向问题。有经验的javaScript开发者都熟悉var self = this或var that = this
这种引用外围的this的模式。但=>
就不须要这种模式了。promise
不管是箭头函数仍是bind,每次被执行都返回的是一个新的函数引用,所以若是你还须要函数的引用去作别的一些事(如卸载监听器),那么你必须保存这个引用。app
ES6支持模板字符串,使得字符串的拼接更加简洁、直观。异步
var name = 'your name is ' + first + ' ' + last;
var name = `your name is ${first} ${last}`;
在ES6中经过${}
就能够完成字符串拼接,只须要将变量放在大括号中。模块化
解构赋值语法是Javascript的一种表达式,能够方便的从数组或对象中快速提取值赋给定义的变量。异步编程
从数据中获取值并赋值到的变量中,变量的顺序与数组中对象顺序对应
var foo = [{"a": "one"}, "two", "three", "four"]; var [one, two, three] = foo; console.log(one, two, three) // 若是你要忽略某些值, 你能够按照下面的写法获取你想要的值 var [first, , , last] = foo; console.log(first, last) var [a, b] = [1, 2] console.log(a, b)
若是没有从数组中获取到值,能够为变量设置一个默认值。
var a ,b; [a= 3, b=7]=[1]
const student = { name: "Nyan", age: 27, city: "ShenZhen" } const {name, age, city} = student; console.log(name, age, city)
延展操做符...
能够在函数调用/数组构造时,将数组表达式或者string在语法层面展开;还能够在构造对象时,将对象表达式按key-value方式展开。
myFunction(...iterableObj)
[...iterableObj, '4', ...'hello', 6]
let objClone = {...obj};
function (x, y, z) { return x + y + z; } const numbers= [1, 2, 3]; console.log(sum.apply(null, numbers)) console.log(sum(...numbers))
const students = ["Jack", "Tom"]; const persons = ["Jony", ...students, "Kin", "Anny"] console.log(persons)
和参数列表的展开相似,...
在构造数组时,能够任意位置屡次使用。
var arr = [1, 2, 4]; var arr2 = [...arr] arr2.push(5) console.log(arr2)
展开语法,与object.assign()行为一致,执行的是都是浅拷贝(只遍历一层)
var arr3 = [...arr1, ...arr3] // 等同与 var arr4 = arr1.concat(arr2)
var obj1 = {foo: "baz", x: 12}; var obj2 = {foo: "bar", y: 34}; var cloneObj = {...obj1}; var mergeObj = {...obj, ...obj2}; console.log(obj1, obj2)
...延展操做符,用于取出参数对象的全部可遍历属性
,来进行传递<CustomComponent name="Nyan" age={27} />
const params = { name: "name", age: 27 } <CustomComponent {...params} />
const params = { name: "name", age: 27, type: "type Name" } var {type, ...other} = params; <CustomComponent {...other} />
在ES6中容许咱们在设置一个对象的属性的时候不指定属性名
const name= "name", age = 27 const student = {name: name,age: age, getName: function(){}}
对象必须包含属性和值,显得很是冗余
const name= "name", age = 27 const student = {name,age, function getName(){}}
Promise是异步编程的一种解决方案,比传统的解决方案callback更加优雅。它最先由社区提出与实现,ES6将其写进了语言标准,统一了写法,原生提供了Promise对象。
setTimeout(function(){ console.log("promise test") setTimeout(function(){ console.log("promise test 1") }, 1000) }, 1000)
var waitSecond = new Promise(function(resolve, reject){ setTimeout(resolve, 1000) }); waitSeccond .then(function(){ console.log("promise test") return waitSecond }).then(function(){ console.log("promise test 1") })