1. 程式人生 > >es6 - 箭頭

es6 - 箭頭

通過 let style ret chan use UNC alc bsp

 1 class User {
 2     constructor(name, age) {
 3         this.name = name;
 4         this.age = age;
 5     }
 6 
 7     changeName(name) {
 8         this.name = name;
 9     }
10 
11     changeAge(age) {
12         this.age = age;
13     }
14 
15     chage(name, age) {
16 
17         //
本this -> 獲取的是全局的this 18 var self = this; 19 let fn = () => { 20       //無論嵌套多少層 -> 只要通過箭頭符號獲取的就是全局的 21 (() => { 22 (() => { 23 (() => { 24 console.log(‘全局this:‘, self); 25 this
.changeAge(age); 26 this.changeName(name); 27 })(); 28 })() 29 })() 30 31 console.log(‘全局this:‘, self); 32 this.changeAge(age); 33 this.changeName(name); 34 } 35 36 //
function fn() { 37 // // 當前 - 帶箭頭 -> 全局 38 // this.changeAge(age); 39 // this.changeName(name); 40 // } 41 42 fn(); 43 } 44 45 46 } 47 48 var user = new User(‘zhang‘, 30); 49 user.chage(‘long‘, 20); 50 51 console.log(‘chage修改後:‘, user); 52 53 54 55 // 函數 -es5 56 function calc(x, y) { 57 return x * y; 58 } 59 60 console.log(calc(1, 5)); 61 62 // 函數 -es6 63 var es6calc = (x, y) => { 64 return x * y; 65 } 66 67 console.log(es6calc(1, 2));

es6 - 箭頭