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.默认参数
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.箭头函数 =&>
const Person = { 'name': 'little bear', 'age': 18, 'sayHello': () => { console.log('我叫' + Person.name + '我今年' + Person.age + '岁!') } } Person.sayHello();
- 10.Promise: 异步编程的一种解决方案,避免了传统的回调函数的层层嵌套,也就是常说的“回调地狱”。