zoukankan      html  css  js  c++  java
  • JavaScript 函数绑定 Function.prototype.bind

    ECMAScript Edition5  IE9+支持原生,作用为将一个对象的方法绑定到另一个对象上执行。

     1 Function.prototype.bind = Function.prototype.bind || function(){
     2     // 该方法当前所属的对象.
     3     var self = this;
     4     var args = Array.prototype.slice.call(arguments);
     5     // 绑定的对象.
     6     var o = args.shift();
     7     // 如上操作后将绑定时的参数也绑定进来.
     8     // 返回一个指定作用域调用的方法.
     9     return function(){
    10         return self.apply(o, args.concat(Array.prototype.slice.call(arguments)));
    11     };
    12 }
    13 var o={
    14     v:100,
    15     show:function(nm1,nm2){
    16         return this.v + "," + nm1 + "," + nm2;
    17     }
    18 };
    19 var o2={
    20     v:200
    21 };
    22 var f=o.show.bind(o2,"test1");
    23 alert(f("test2"));
  • 相关阅读:
    学无止境,我爱python
    Flask目录
    Django目录
    前端目录
    python目录
    MySQL目录
    Oracle与Sql server的区别
    Git
    restful规范
    Django 解决跨域问题(写入到中间件中)
  • 原文地址:https://www.cnblogs.com/xf_z1988/p/javascript_function_prototype_bind.html
Copyright © 2011-2022 走看看