Function.prototype.bind = function () {//this points to all functions generated by the function constructorvar self = This,//save original function//[] is equivalent to Array.prototype (use the Shift method on the array constructor prototype to take the first parameter passed as the object that this point points to)context = [].shift.call (arguments),//The This context that needs to be boundthe//slice function is to return the selected array element ( here [].slice.call (arguments) is the parameter passed within the BIND function ) args = [].slice.call (arguments); Remaining parameters to the array return function () {//return a new function//Here [].slice.call (arguments) is the parameter that needs to inherit the new function of the bind function Return self.apply (context, [].concat.call (args, [].slice.call (arguments)));when executing a new function, the previously passed context is treated as this in the new function body, and the concat function is used to connect two separate arguments .//And combine two parameters passed in separately as parameters of the new function}};var obj = {name: ' Sven '};var func = function (A, B, C, D) {alert (this.name);//output: Svenalert ([A, B, C, d])//input Out: [1, 2, 3, 4]}.bind (obj, 1, 2); func (3, 4);
The bind principle comes with some of your own understanding (quoted in JavaScript design pattern and with view practice)