ES6~ES8 新功能

ES6:

1. 箭头函数
// 表达体
var odds = evens.map(v => v + 1);
var nums = evens.map((v, i) => v + i);
var pairs = evens.map(v => ({even: v, odd: v + 1}));

// 申明体
nums.forEach(v => {
  if (v % 5 === 0)
    fives.push(v);
});

// 词法 this
var bob = {
  _name: "Bob",
  _friends: [],
  printFriends() {
    this._friends.forEach(f =>
      console.log(this._name + " knows " + f));
  }
}
2. 类class
class Home extends React.Component {
    constructor(props) {
        super(props);
        this.state = {}
    }
}
3. 增强的对象字面量
var obj = {
    // __proto__
    __proto__: theProtoObj,
    // 'handler: handler'缩写
    handler,
    // 方法
    toString() {
     // Super 调用
     return "d " + super.toString();
    },
    // 计算(动态)属性名称
    [ 'prop_' + (() => 42)() ]: 42
};
4. 模板字符串
// 基本字符串字面量创建
`In JavaScript '\n' is a line-feed.`

// 多行字符串
`In JavaScript this is
 not legal.`

// 字符串插值
var name = "Bob", time = "today";
`Hello ${name}, how are you ${time}?`
5. 解构
[a, b] = [b, a]
6. 默认值+rest参数+扩展运算符
let data = {...a, ...b}
7. 新的变量申明方式 let + const
8. 迭代器 Iterators + for..of
9. 生成器Generators
var fibonacci = {
  [Symbol.iterator]: function*() {
    var pre = 0, cur = 1;
    for (;;) {
      var temp = pre;
      pre = cur;
      cur += temp;
      yield cur;
    }
  }
}
for (var n of fibonacci) {
  if (n > 1000)
    break;
  console.log(n);
}
10. Unicode
"".match(/./u)[0].length == 2
"\u{20BB7}"==""=="\uD842\uDFB7"
11. 模块Modules
12. 模块加载器
13. map + set + weakmap + weakset
// Sets
var s = new Set();
s.add("hello").add("goodbye").add("hello");
s.size === 2;
s.has("hello") === true;

// Maps
var m = new Map();
m.set("hello", 42);
m.set(s, 34);
m.get(s) == 34;

// Weak Maps
var wm = new WeakMap();
wm.set(s, { extra: 42 });
wm.size === undefined

// Weak Sets
var ws = new WeakSet();
ws.add({ data: 42 });
// 因为添加的对象没有其他引用,所以它不会被保存在集合中
14. 代理Proxies
15. 符号Symbols
var MyClass = (function() {
  // module scoped symbol
  var key = Symbol("key");
  function MyClass(privateData) {
    this[key] = privateData;
  }
  MyClass.prototype = {
    doStuff: function() {
      ... this[key] ...
    }
  };
  return MyClass;
})();

var c = new MyClass("hello")
c["key"] === undefined
16. 可分类的内置插件
17. Math + Number + String + Array + Object APIs
Number.EPSILON
Number.isInteger(Infinity) // false
Number.isNaN("NaN") // false

Math.acosh(3) // 1.762747174039086
Math.hypot(3, 4) // 5
Math.imul(Math.pow(2, 32) - 1, Math.pow(2, 32) - 2) // 2

"abcde".includes("cd") // true
"abc".repeat(3) // "abcabcabc"

Array.from(document.querySelectorAll('*')) // Returns a real Array
Array.of(1, 2, 3) // Similar to new Array(...), but without special one-arg behavior
[0, 0, 0].fill(7, 1) // [0,7,7]
[1, 2, 3].find(x => x == 3) // 3
[1, 2, 3].findIndex(x => x == 2) // 1
[1, 2, 3, 4, 5].copyWithin(3, 0) // [1, 2, 3, 1, 2]
["a", "b", "c"].entries() // iterator [0, "a"], [1,"b"], [2,"c"]
["a", "b", "c"].keys() // iterator 0, 1, 2
["a", "b", "c"].values() // iterator "a", "b", "c"

Object.assign(Point, { origin: new Point(0,0) })
18. Promises
function timeout(duration = 0) {
    return new Promise((resolve, reject) => {
        setTimeout(resolve, duration);
    })
}
var p = timeout(1000).then(() => {
    return timeout(2000);
}).then(() => {
    throw new Error("hmm");
}).catch(err => {
    return Promise.all([timeout(100), timeout(200)]);
})
19. 二进制和八进制字面量
0b111110111 === 503 // true
0o767 === 503 // true
20. Reflect API
21. 尾调用

ES7:

1. Array.prototype.includes()
2. 指数运算符

ES8:

1. Async函数

你可能感兴趣的:(ES6~ES8 新功能)