ES6新特性
- 1.類(支持繼承 extends)
class TestClass { constructor() { // class constructor this.name = 'Basexxx' } getName() { // class method //console.log(`Class name: ${this.name}`) alert(`Class name: ${this.name}`); } } new TestClass().getName();
- 2.默認參數(shù)
function xxx(height = 50, color = 'red', url = 'http://azat.co'){...}
- 3.模塊化(import和export運算符),firefox測試成功
#module.js export function foo() { return '1111'; } export var name='22222'; #test.html <script type="module"> import { foo,name } from './module.js'; alert(foo()); alert(name); </script>
- 4.塊級作用域的let和const
let命令聲明變量饲趋,類似var蚕泽,但所聲明的變量只在let代碼塊有效#let { var a=13; let b=19; } console.log(a); console.log(b); #const const foo = 'xxx';
- 5.模版表達式
var name = `test xx ${height} ff ${color} ${url}`;
- 6.多行字符串
var roadPoem = `aaa bbb`;
- 7.拆包表達式
ajax返回: var { house, mouse} = data; // 我們會拿到house和mouse的值的
- 8.改進的對象表達式
var serviceBase = {port: 3000, url: 'azat.co'}, getAccounts = function(){return [1,2,3]} var accountService = { __proto__: serviceBase, getAccounts, toString() { return JSON.stringify((super.valueOf())) }, getUrl() {return 'http://' + this.url + ':' + this.port}, [ 'valueOf_' + getAccounts().join('_') ]: getAccounts() }; console.log(accountService)
- 9.箭頭函數(shù) =&>
const Person = { 'name': 'little bear', 'age': 18, 'sayHello': () => { console.log('我叫' + Person.name + '我今年' + Person.age + '歲!') } } Person.sayHello();
- 10.Promise: 異步編程的一種解決方案缰冤,避免了傳統(tǒng)的回調(diào)函數(shù)的層層嵌套集歇,也就是常說的“回調(diào)地獄”。