zoukankan      html  css  js  c++  java
  • 箭头函数普通函数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>
  • 相关阅读:
    event 事件 键盘控制div移动
    event 事件 div鼠标跟随
    获取坐标封装 getPos
    event 事件 clientX 和clientY 配合scrollTop使用, div跟着鼠标走
    event 事件 冒泡
    event 事件 坐标兼容
    event事件基础 document
    DOM 多字符搜索
    DOM search table 模糊搜索
    Reverse a sentence
  • 原文地址:https://www.cnblogs.com/yaowen/p/7094817.html
Copyright © 2011-2022 走看看