es6 - filter for-chrome
阿新 • • 發佈:2018-07-31
span develop inter 對象 () 回調 ... square ber
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 // 回調拿出name 29 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 = this 45 } 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);
es6 - filter for-chrome