1 2 'use strict'; 3 let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 4 5 6 // 除去取余2的 - es6 7 let es5OddNumbers = numbers.filter(function (number) { 8 return number % 2; 9 });10 console.log(es5OddNumbers);11 12 // 除去取余2的 - es6 - 第二种写法13 let es6OddNumbers = numbers.filter(number => number % 2);14 console.log(es6OddNumbers);15 16 17 18 // 和es5不一样的函数返参19 let square = x => x * x;20 console.log(square(10));21 22 // let add = (形参1,形参2,...)23 let add = (a, b) => a + b;24 console.log(add(3, 4));25 26 // 数组对象27 let developers = [{ name: 'Rob' }, { name: 'Jake' }];28 // 回调拿出name29 let es5Output = developers.map(function (developer) {30 return developer.name;31 });32 console.log(es5Output);33 34 let es6Output = developers.map(developer => developer.name);35 console.log(es6Output);36 37 38 39 // es5 倒计时40 function CounterES5() {41 this.seconds = 0;42 window.setInterval(function () {43 this.seconds++;44 }.bind(this), 1000); // or }.bind(this), 1000) and skip that = this45 }46 var counterA = new CounterES5();47 window.setTimeout(function () {48 console.log(counterA.seconds);49 }, 1200);50 51 // es6倒计时52 function CounterES6() {53 this.seconds = 0;54 window.setInterval(() => this.seconds++, 1000);55 }56 57 let counterB = new CounterES6();58 window.setTimeout(() => console.log(counterB.seconds), 1200);