Simple implementation to see the following example:

Let objA = {name: "xiao brother ", age: 20, objAFun: Function (form, to) {console.log(' name: ${this.name}, age: ${this.age}, from: ${form}, destination: ${to}. `); }} let objB = {name: "seven leaves one flower ", age: 23} obja.objun.mycall (objB," gz ", "gz "); Obja.objafun. MyApply (objB, [" Shanghai "]); Obja.objafun.mybind (objB, [" Beijing "])();Copy the code

1. Implement the call function

Function. The prototype. MyCall = Function (context) {/ / determining if a call object objAFun Function if (typeof this! == 'function') { throw TypeError("typeof error"); } let args = [...arguments].slice(1); let result = null; / / whether the context context into, or the context is set to the object function = context | | window; // Set the calling object to the object function context.fn = this; // Call the function result = context.fn(... args); // Delete the attribute delete context.fn; return result; }Copy the code

2. Implement the apply function

Function.prototype.myApply = function(context) { if (typeof this ! == 'function') { throw TypeError("type error"); } context = context || window; context.fn = this; let result = null; If ([...arguments][1]) {result = context.fn([...arguments][1])} else {result = context.fn(); } delete context.fn; return result; }Copy the code

3. Implement bind

Function.prototype.myBind = function(context) { if (typeof this ! == "function") { throw TypeError("type error"); } let args = [...arguments].slice(1); let fn = this; Return Fn. Apply (this instanceof Fn? this : context, args.concat(... arguments)); }}Copy the code