首页 > 其他 > 详细

箭头函数的this指向问题

时间:2018-06-22 17:17:29      阅读:168      评论:0      收藏:0      [点我收藏+]

this指向的固定化,并不是因为箭头函数内部有绑定this的机制,实际原因是箭头函数根本没有自己的this,导致内部的this就是外层代码块的this。正是因为它没有this,所以也就不能用作构造函数。

箭头函数转成ES5的代码如下。

function foo() {
  setTimeout(() => {
    console.log(‘id:‘, this.id);
  }, 100);
}

// ES5
function foo() {
  var _this = this;

  setTimeout(function () {
    console.log(‘id:‘, _this.id);
  }, 100);
}


作者:紫陌兰溪
链接:https://www.jianshu.com/p/ee382fad8a9c
技术分享图片

 

 
https://www.jianshu.com/u/c9a4863462fc

箭头函数的this指向问题

原文:https://www.cnblogs.com/qiqi105/p/9214209.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!