JavaScript ~ Core Implementation - rohit120582sharma/Documentation GitHub Wiki

Function.prototype.bind

/* Bind method */
Function.prototype.bind = function(newContext){
	console.dir(this);
	console.dir(arguments.callee);

	var self = this;
	var callee = arguments.callee;
	var args = Array.prototype.slice.call(arguments, 1);
	
	return function(){
		sumArgs = args.concat(Array.prototype.slice.call(arguments, 0));
		return self.apply(newContext, sumArgs);
	}
}
/* exercise */
function sum(a, b){
	return (a + b);
}
var firstObj = {
	x: 3,
	y: 3,
	sum: function(){
		return (this.x + this.y);
	}
};
var secondObj = {
	x: 2,
	y: 2,
	sum: function(){
		return (this.x + this.y);
	}
};

var bound = firstObj.sum.bind(secondObj, 5);
setTimeout(function(){
	console.log(bound(3));
}, 2000);