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));