bind
_.bind(function,context)
- 给一个object绑定一个function,任何时候调用方法,this都指向这个object
官方例子:
var func = function(greeting){ return greeting + ":" + this.name; } func = _.bind(func,{name:"moe"},"hi"); func(); // "hi : moe"
源码:
var ctor = function(){}; //ECMA 5支持的 var nativeBind = Function.prototype.bind, slice = Array.prototype.slice; _.bind = function(func,context){ var args, bound; //如果原生支持,就采用原生的bind if(func.bind === nativeBind && nativeBind){ return nativeBind.apply(func,slice.call(arguments,1)); } //对func的类型进行判断,非function就抛异常 if(!_.isFunction(func)){ throw new TypeError; } //第3个参数转换成数组 args = slice.call(arguments,2); return bound = function(){ //判断当前this是否是function的实例 if(!(this instanceof bound)){ return func.apply(context,args,concat(slice.call(arguments))); } //保证this为func的实例 ctor.prototype = func.prototype; var self = new ctor; ctor.prototype = null; var result = func.apply(self,args.concat(slice.call(arguments))); //判断result是不是Object if(Object(result) === result){ return result; } return self; }; };
相关推荐
因为underscore本来就是为了充分发挥JavaScript的函数式编程特性,所以也提供了大量JavaScript本身没有的高阶函数。本文重点给大家介绍underscore之function知识,感兴趣的的朋友一起学习吧
Explore the Underscore.js library by example using a test-driven development approach About This Book Understand and learn to apply functional programming principles using the built-in functions of ...
Explore the Underscore.js library by example using a test-driven development approach About This Book Understand and learn to apply functional programming principles using the built-in functions of ...
Underscore.js是目前最好用的Javascript工具包,提供了100多个函数。Underscore.php是Underscore.js的PHP移植版,并且实现了部分Underscore.js中的功能! 我花了一个月的时间将Underscore.php的文档翻译为中文,开源...
underscore.js的开发文档,需要使用的朋友们可以看看
underscore-min.js
underscore.js underscore.js underscore.js
underscore源码学习计划
Underscore1.8.3 参考手册.CHM,Underscore中文教程,Underscore离线手册
underscore js工具类,underscore为集合类对象提供了一致的接口
underscore源码解析
underscore-1.6.0.zip
underscore-1.4.3.zip
underscore.js api文档 chm
1.2.3的Underscore中文文档,采用网页版压缩制成的chm文件,方便查看,方便点击链接
使用第三方模块Underscore.js,Immutable.js,UUID(源代码+截图)使用第三方模块Underscore.js,Immutable.js,UUID(源代码+截图)使用第三方模块Underscore.js,Immutable.js,UUID(源代码+截图)使用第三方模块...
underscore javascript库 使用教程.zip
underscore.js源码,适合初始化学习javascript,强烈推荐