prototype.js中的Function.prototype.bind方法:

Function.prototype.bind = function() {
var __method = this;
var args = Array.prototype.slice.call(arguments);
var object=args.shift();
return function() {
return __method.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
}
}

为所有function对象增加一个新的prototype(原型)方法bind:

  • 将调用bind方法的对象保存到__method(方法)变量里面。
  • 将调用bind方法时传递的参数转换成为数组保存到变量args。
  • 将args数组的第一位[0]元素提取出来保存到变量object。
  • 返回一个函数。

这个被返回的函数在再次被调用的时候执行如下操作:

  • 使用apply方法将调用bind方法的函数里面的this指针替换为object。
  • 将传递到这个匿名函数里面的参数转换为数组,与args数组组合形成一个新的数组,传递给__method方法。

例子:

function o(){
this.num = 1;
var fn = function(arg){alert(this.num+arg)}.bind(this,2);
fn(); //alert(3)
}
new o()

最新文章

  1. Liferay7 BPM门户开发之38: OSGi模块化Bndtools、Maven、Gradle开发构建入门
  2. WEB前端html基础中的各类标签介绍
  3. 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II
  4. ntko office 5.0.1.0注册码
  5. js中的script标签
  6. PMP考试--关于职业道德
  7. c#中设置按钮Button为透明
  8. Ubuntu eclipse :An error has occurred. See the log file
  9. linux 脚本測试网络速度
  10. C#中制作MDI窗体
  11. OpenCV混合高斯模型函数注释说明
  12. rabbitmq - (消息队列) 的基本原理介绍
  13. Angular4学习笔记(七)- ViewChild和ViewChildren
  14. JAVA 课堂测试
  15. PWM输出
  16. Java编解码分析
  17. MongoDB MapReduce 的示例。
  18. 团队作业4 Alpha冲刺
  19. spring Controller 层注解获取 properties 里面的值
  20. ssh框架,工具类调用service层方法

热门文章

  1. Hive配置项的含义详解
  2. Websocket 临时参考网站
  3. 论 Python Opencv 中文路径及中文文件名图像文件读取的两种方式
  4. 3.MUI端APP获取Json数据,并且实现遍历
  5. wmware 10 升级到11后,macos不能运行的问题
  6. 最优布线问题(wire.cpp)
  7. 用php实现简单的自制计算器
  8. selenium自动化之js处理点击事件失效
  9. Python接口测试实战3(上)- Python操作数据库
  10. K-means算法实现