Arrow function

箭头函数在没有使用this时,和普通函数没有区别

看this指向,先看在哪一个函数的大括号里面

this的问题,本质上是函数作用域的问题,this不能只看函数声明的声明,更要看函数在哪里调用,找到函数的使用者

 
<script>
  var x = 5;

        function show(){
            return () => {
                return this.x + 2
            }
        }

        let oYm = {x:1};
        let oCy = {x:2};

        oYm.fn = show();

        console.log(oYm.fn.call(oCy));// 7
</script>
 

posted @ 2021-04-22 09:13  源大大123  阅读(44)  评论(0)    收藏  举报