箭头函数与this

ECMAScript 2015 函数新玩法

 浏览器环境下(chrome)

箭头函数中的使用this 是依赖于当前所属作用域,在箭头函数在创建时就引用了所在上下文中的this

bind,call,apply这三种方法,不会对简头函数中的作用域中的this产生任何变化

 

全局情况下 为 window

场景1
((a)=>console.log(this) )() //window
加上 "use strict"
(()=>{"use strict"; console.log(this))() //window
场景2 let obj = {   aid : 123456,   arr:()=>console.log(this),
  
  arr2:function(){
       
       (()=>{console.log(this)})()   
      }     arr3:function(){
      document.onclick=event=>console.log(this);
   } } obj.arr();
// window

obj.arr2(); //obj

obj.arr3(); //obj 绑定click事件后this还是当前所在作用域,而非是事件绑定的document

  


 

posted @ 2017-10-15 17:42  慧宁师  阅读(134)  评论(0编辑  收藏  举报