1、箭頭函數 this
1.不帶參數 var test = function(){ return 'test'; } test() //結果是:'test' 等價於 let test = ()=>test; test(); //結果是:'test' 2.帶參數 var test = function(x){ return x; } test(111); //結果是: 111 等價於 let test = x => x; test(222); //結果是: 222 3.數組循環 let n = [1,3,5,7,10]; let m = []; n.forEach( v => { if ( v % 5 === 0) { m.push(v); } }) console.log(m); // 結果是: [5, 10] 4.箭頭函數中的 this 指的不是window,是對象自己 function test (){ let a = 1; setTimeout(() => { this.a++; console.log(a); }, 1000) } test(); // 結果是: 1
2、let 和 const
let 和 const 只在最近的一個塊中(花括號中)有效 const 用來聲明一個常量,但也並不是一成不變的 var a = 1; { let a = 2; console.log(a); // 結果 :2 } console.log(a); // 結果: 1 const obj = [1,3]; obj.push = 4; console.log(obj); // [1, 3, push: 4] obj = 666; // 報錯
3、class 類
class Animal { constructor(){ console.log('我是一個動物'); } } class Person extends Animal { constructor(){ super(); console.log('我是一個程序員'); } } let aa = new Person();