Handwriting call, bind, apply

			// achieve Call 
			var = that the this; // applet environment 
			function mySymbol (obj) { 
				the let UNIQUE = (Math.random () + new new a Date () the getTime ().) ToString (32) .slice (0,8. ); 
				IF (obj.hasOwnProperty (UNIQUE)) { 
					return mySymbol (obj) 
				} the else { 
					return UNIQUE; 
				} 
			} 
			the let the Person = { 
				name: 'Tom', 
				say (Age = 23 is, City = 'Ningbo', job = ' front-end ') { 
					console.log ( `my name is $ {this.name}, year, $ {age}, at $ {city}, the Job {}` in $) 
				} 
			} 
			the let Person1 = { 
				name:' Tom1 ' 
			} 
			Function = function .prototype.MyCall (context) { 
				context = || that context; // PC environment environment applet context = context || window;
				Fn = mySymbol the let (context); 
				context.fn = the this; 
				the let Arg = [arguments ...] .slice (. 1); 
				context.fn (... Arg); 
				Delete context.fn; 
			} 
			Person.say.MyCall (Person1,23, 'Ningbo', 'front end'); 
			Person.say.call (Person1,23, 'Ningbo', 'front end'); 
			// implemented Apply 
			
			Function.prototype.myApply = function (CXT, args) { 
				    cxt = cxt || that; // applet environment cxt = cxt || that; PC environment CXT = || window CXT; 
				    var Fn = mySymbol (CXT); 
				    CXT [Fn] = the this; 
				    var = Result CXT [ Fn] (args ...); 
				    return Result; 
			} 
			Person.say.myApply (of Person1, [ '23 is',' Ningbo ',' front end ']) 
			Person.say.apply (of Person1, [' 23 is', 'Ningbo ''front end'])
			 
			the Function.prototype.myBind = function(context){
				let self = this;
				let arg = [...arguments].slice(1);
				return function(){
					let newArg = [...arguments];
					return self.apply(context,arg.concat(newArg))
				}
			}
			
			let fn = Person.say.myBind(Person1,23);
			fn();
			fn('宁波','前端')

  Record learning

Guess you like

Origin www.cnblogs.com/zwyboom/p/11791711.html