zoukankan      html  css  js  c++  java
  • 给函数绑定一个特定上下文

    <!DOCTYPE html>
    <html>
    <head>
    <title>给函数绑定一个特定上下文</title>
    <meta charset="utf-8">
    </head>
    <body>
    <button id="test">Click me</button>
    <script type="text/javascript">

    function bind(context,name){//用于创建并返回一个匿名函数,该函数使用apply调用了原始函数,以便强制将上下文设置成我们想要的任何对象。本例中,传给bind()的第一个参数就是要设置的上下文对象。上下文context和方法名称name。通过匿名函数的闭包进行传入,在函数结束时进行调用,而匿名函数闭包则包含了传递给bind的参数
    return function(){//返回一个匿名函数
    return context[name].apply(context,arguments);//个人认为return在这里有无结果一样
    }

    }
    var button={
    clicked:false,
    click:function(){
    this.clicked=true;
    console.log(this);
    console.log(button.clicked);
    }
    }

    var elem=document.getElementById('test');
    elem.addEventListener('click',bind(button,'click'),false);
    </script>
    </body>
    </html>

  • 相关阅读:
    Java I/O
    iOS AppsFlyer的使用注意事项
    Star Schema and Snowflake Schema
    SSB基准测试
    ES Route
    CPS(Cyber-Physical Systems)白皮书-摘选
    蓄电池放电容量与环境温度的关系
    时间序列分析(二)
    时间序列分析(一)
    IndexR
  • 原文地址:https://www.cnblogs.com/superZz/p/5791106.html
Copyright © 2011-2022 走看看