不需要使用call 和 apply 方法来模拟实现 ES5 的 bind 方法
// The first version Function.prototype.applyOne = function(context) { // First, get the function that is called with call, using this to get it. context.fn = this; context.fn(); delete context.fn; }
// Write a simple demo without parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyOne(lulin) // lulin It's not easy! // The second version Function.prototype.applyTwo = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyTwo(lulin, [24]) // lulin 24 It's not easy! // The third version Function.prototype.applyThree = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyThree(lulin, [24]) // lulin 24 It's not easy! // The fourth version Function.prototype.applyFour = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyFour(lulin, [24]) // lulin 24 It's not easy! // The fifth version Function.prototype.applyFive = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyFive(lulin, [24]) // lulin 24 It's not easy! // The sixth version Function.prototype.applySix = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applySix(lulin, [24]) // lulin 24 It's not easy! // The seventh version Function.prototype.applySeven = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applySeven(lulin, [24]) // lulin 24 It's not easy! // The eighth version Function.prototype.applyEight = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyEight(lulin, [24]) // lulin 24 It's not easy! // The ninth version Function.prototype.applyNine = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyNine(lulin, [24]) // lulin 24 It's not easy! // The tenth version Function.prototype.applyTen = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyTen(lulin, [24]) // lulin 24 It's not easy! // The eleventh version Function.prototype.applyEleven = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyEleven(lulin, [24]) // lulin 24 It's not easy! // The twelfth version Function.prototype.applyTwelve = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyTwelve(lulin, [24]) // lulin 24 It's not easy! // The thirteenth version Function.prototype.applyThirteen = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyThirteen(lulin, [24]) // lulin 24 It's not easy! // The fourteenth version Function.prototype.applyFourteen = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyFourteen(lulin, [24]) // lulin 24 It's not easy! // The fifteenth version Function.prototype.applyFifteen = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyFifteen(lulin, [24]) // lulin 24 It's not easy! // The sixteenth version Function.prototype.applySixteen = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applySixteen(lulin, [24]) // lulin 24 It's not easy! // The seventeenth version Function.prototype.applySeventeen = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applySeventeen(lulin, [24]) // lulin 24 It's not easy! // The eighteenth version Function.prototype.applyEighteen = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyEighteen(lulin, [24]) // lulin 24 It's not easy! // The nineteenth version Function.prototype.applyNineteen = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyNineteen(lulin, [24]) // lulin 24 It's not easy! // The twentieth version Function.prototype.applyTwenty = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyTwenty(lulin, [24]) // lulin 24 It's not easy! // The twenty-first version Function.prototype.applyTwentyOne = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyTwentyOne(lulin, [24]) // lulin 24 It's not easy! // The twenty-second version Function.prototype.applyTwentyTwo = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyTwentyTwo(lulin, [24]) // lulin 24 It's not easy! // The twenty-third version Function.prototype.applyTwentyThree = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyTwentyThree(lulin, [24]) // lulin 24 It's not easy! // The twenty-fourth version Function.prototype.applyTwentyFour = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyTwentyFour(lulin, [24]) // lulin 24 It's not easy! // The twenty-fifth version Function.prototype.applyTwentyFive = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console.log(this.name,age); } };
var lulin = { name: "lulin", };
// Let's see the result: jawil.sayHello.applyTwentyFive(lulin, [24]) // lulin 24 It's not easy! // The twenty-sixth version Function.prototype.applyTwentySix = function(context, args) { // First, get the function that is called with call, using this to get it. context.fn = this; // Then, pass the arguments to the function. context.fn.apply(context, args); // Finally, delete the function. delete context.fn; }
// Write a simple demo with parameters var jawil = { name: "jawil", sayHello: function(age) { console
内容来源: jawil/blog