首页 > 其他 > 详细

箭头函数普通函数this

时间:2017-06-29 16:40:55      阅读:256      评论:0      收藏:0      [点我收藏+]
<!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>

 

箭头函数普通函数this

原文:http://www.cnblogs.com/yaowen/p/7094817.html

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