[原神]策劃教你規劃你的錢包
阿新 • • 發佈:2022-03-31
this指向兩句話口訣:
1.this永遠指向函式執行的環境,而不是宣告的環境
2.箭頭函式的this指向父級
一、正常函式的this
function fn1(){ console.log(this) } fn1() //this===window fn1.call({x:100}) //this==={x:100} const fn2 = fn1.bind({x:200}) fn2() //this==={x:200}
二、物件的this和箭頭函式的this
const zhangsna = { name:'張三', sayHi(){ console.log(this) //this是當前物件} wait(){ settimeOut(function(){ console.log(this) //this是window }) } } const zhangsna = { name:'張三', sayHi(){ console.log(this) //this是當前物件 } wait(){ settimeOut(()={ console.log(this) //this是當前物件 }) } }
三、建構函式的this
1 class People { 2 constructor(name){3 this.name = name 4 this.age = age 5 } 6 sayHi(){ 7 console.log(this) 8 } 9 } 10 11 let person = new People('張三') 12 person.sayHi() //person物件