箭头函数普通函数this

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
    <script src="https://google.github.io/traceur-compiler/bin/BrowserSystem.js"></script>
    <script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
</head>

<script  type="text/traceur">
window.onload=function(){
    
    /*var d = {b:1};
    d.a = function (){
        (() => {
            console.log(this.b); //1
        })();
    }
    d.a();
    
    alert(456); */
    
    b = 2000;
    
    var d1 = {b:1};
    var d2 = {b:11};
    d1.aa = function (f){
        f();  //2000
        //f.call(d2);   //箭头函数即使是call仍然是定义时的window,普通函数用call调用改变this,普通函数在调用处决定this,箭头函数在定义时决定this,
        (() => {
            //console.log(this); //d1
        })();  
    }
    /*d1.aa(() => {
            console.log(this);  //Window,调用相当于是在window中定义的函数,函数定义的时候参数是加入了局部函数作用域
        }); */ 
    d1.aa(function(){
            console.log(this);  //Window
        });  
    /*d1.aa((function(){
            console.log(this);  //Window
        })());*/ 
    
}

</script>
<body>
<div id="app-3">
</div>
</body>
</html>
原文地址:https://www.cnblogs.com/yaowen/p/7094817.html