【javascript】监听函数中的this指向

1、注意this指向问题,在addeventlistener监听事件中,this不再是原来的this,而是事件源

Enlarge.prototype.move = function(){let that = this//解决办法1、提前用that保存thisthis.show.addEventListener('mouseover',function(){console.log(this);//使用thatthat.enlarge.classList.add('active');that.mask.classList.add('active');})//解决办法2,使用箭头函数,里面的this还是原来的thisthis.show.addEventListener('mouseout',()=>{this.enlarge.classList.remove('active');this.mask.classList.remove('active')})}

【javascript】监听函数中的this指向

1、注意this指向问题,在addeventlistener监听事件中,this不再是原来的this,而是事件源

Enlarge.prototype.move = function(){let that = this//解决办法1、提前用that保存thisthis.show.addEventListener('mouseover',function(){console.log(this);//使用thatthat.enlarge.classList.add('active');that.mask.classList.add('active');})//解决办法2,使用箭头函数,里面的this还是原来的thisthis.show.addEventListener('mouseout',()=>{this.enlarge.classList.remove('active');this.mask.classList.remove('active')})}