Call implementation summary:
- Sets the function as a property of the object
- Execute the &delete function
- Execute the function by specifying this to the function and passing in the given argument
- If no argument is passed, the default point is window
# # # call to realize the Function. The prototype. MyCall = Function (context = Windows) {context. The fn = this caller / / save Function let the args = [...arguments].slice(1) // Let result = context.fn(... Return result} let obj = {age: Function bar () {console.log(sex,name,this.age)} bar.mycall (obj,' male ', 'CZK ') // male CZKCopy the code
Apply: the same as call, except that the format of the parameter is different when the method is executed
# # # the apply to realize the Function. The prototype. MyApply = Function (context = Windows) {context. The fn = this let the args = [...argments].slice(1) let result = context.fn(args) return result }Copy the code
The implementation of bind:
- The first thing to understand is that bind creates a new function and when the new function is called the first argument to bind() will be this when it runs
- The following series of arguments are passed as arguments before the argument
- When called, bind returns the same function body as before except that the reference to this has been changed and the arguments that bind passed are passed to the function that returned
# # # bind to realize the Function. The prototype. MyBind = Function (context = Windows) {let that = this let the args = [r].... the arguments slice (1) return function () { that.apply(context, args.concat([...arguments])) } } let obj = { value : 99 } function bar (name , age) { console.log(name, age, this.value) } let getBar = bar.myBind(obj, 'czk') getBar('smile') // czk smile 99Copy the code