才學(xué)react幾天求問(wèn)大神react的this問(wèn)題classAppextendsReact.Component{//constructor(props){//super(props)//}state={name:'ls'}render(){return點(diǎn)擊觸發(fā)事件}fn(){console.log(1);console.log(this);}}為什么點(diǎn)擊之后這里的this是打印的undefined而不是這個(gè)實(shí)例我知道4個(gè)解決辦法但是我就是搞不懂為什么this指向的undefined按理說(shuō)這里this.fn觸發(fā)了不就是對(duì)象里面方法的調(diào)用嗎既然這樣方法里面的this不就是指向這個(gè)對(duì)象的嗎為什么會(huì)是undefined聽(tīng)大佬說(shuō)這個(gè)this指的是點(diǎn)擊這個(gè)事件所以是undefined那我用箭頭函數(shù)為什么就又可以了(箭頭函數(shù)沒(méi)有this,this會(huì)不斷往上作用域鏈尋找)classAppextendsReact.Component{//constructor(props){//super(props)//}state={name:'ls'}render(){return點(diǎn)擊觸發(fā)事件}fn=()=>{console.log(1);console.log(this);}}
萌新求教!react里面的this指向問(wèn)題求解答!
人到中年有點(diǎn)甜
2019-10-08 09:51:00