1、用let代替var聲明變量javascript
ES5中,咱們能夠在代碼中任意位置聲明變量,甚至能夠重寫已經聲明的變量,ES6引入了一個let關鍵字,它是新的var。java
let language = 'javascript'; let language = 'zj'; //此處會報錯 console.log(language);
由於同一做用域中let已經聲明過了,因此再次聲明會報錯es6
2、常量數組
ES6還引入了const關鍵字,和let用法同樣,惟一的區別就是,const變量是隻讀的函數
3、模板字符串拼接this
用反引號拼接字符串spa
let language = 'javascript'; console.log(language); let lan = `此處寫字符串 ${language}`; console.log(lan) //此處寫字符串 javascript
只要把變量寫在${}裏面就行了;模板字符串也能夠識別空格,可能夠用於多行的字符串,不再用寫\n了。prototype
4、箭頭函數code
let circle = (x) => { const PI = 3.14; let area = PI * r * r; return area; }
能夠省略掉關鍵字function,若是函數只有一條語句,能夠連關鍵字都省略掉對象
let circle2 = (x) => 3.14 * r * r;
5、函數參數的默認值
let sum = (x = 1, y = 2, z = 3) => x + y + z;
console.log(sum());
能夠聲明函數參數的默認值
6、聲明展開和剩餘參數
ES6展開操做符 ...
let params = [1,2,3]; console.log(...params); //1 2 3 let pro = { one:0, two:1, three:2, }; console.log({...pro}); //{ one: 0, two: 1, three: 2 }
7、數組解構
var [a, b] = ['x', 'y'];
以上代碼和下面的代碼效果是同樣的
a = 'x';
b = 'y';
數組解構也能夠進行值的互換
[x, y] = [y, x];
8、使用面向對象biancheng
//ES5語法 function Book (title, page, isbn) { this.title = title; this.page = page; this.isbn = isbn; } Book.prototype.printTitle = function () { console.log(this.title); };
//ES6語法 class Desk { constructor (title, pages, isbn){ this.title = title; this.page = page; this.isbn = isbn; } printIsbn () { console.log(this.isbn); } }
ES6只須要使用class關鍵字,聲明一個有constructor函數和諸如printIsbn等其餘函數的類;
咱們能夠用extends擴展一個類並繼承它的行爲
//ES6語法 class ITDesk extends Desk { constructor (title, pages, isbn){ this.title = title; this.page = page; this.isbn = isbn; } printIsbn () { console.log(this.isbn); } }
更多es6細節請看: http://es6.ruanyifeng.com/#docs/array