码迷,mamicode.com
首页 > 其他好文 > 详细

underscore functions

时间:2015-08-17 21:41:48      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

_.bind(function, object, [*arguments]) :绑定function到object,任何时候调用函数,都指向这个object。不能绑定两个对象。没看明白

其实apply和call也能改变this的指定,只是bind之后,可以省略掉apply和call

_.bind = function bind(func, context) {
	var bound, args;
	if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
	if (!_.isFunction(func)) throw new TypeError;
	args = slice.call(arguments, 2);
	return bound = function() {
	  //什么时候this instanceof bound = true?
	  if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
	  ctor.prototype = func.prototype;
	  var self = new ctor;
	  var result = func.apply(self, args.concat(slice.call(arguments)));
	  if (Object(result) === result) return result;
	  return self;
	};
};

 

underscore functions

标签:

原文地址:http://www.cnblogs.com/wang-jing/p/4737609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!