javascript bind 函数

Function.prototype.bind = function() {
	var self = this,
		context = [].shift.call(arguments),
		args = [].slice.call(arguments);
	return function() {
		return self.apply(context, [].concat.call(args, [].slice.call(arguments)));
	}
}

// test
var obj = {
	name: 'mingzhanghui'
};
var func = function(a, b, c, d) {
	console.log(this.name);
	console.log([a,b,c,d]);
}.bind(obj, 1, 2);

func(3,4);

  

猜你喜欢

转载自www.cnblogs.com/mingzhanghui/p/9208056.html