This is the first day of my participation in Gwen Challenge.

In fact, the knowledge point asked in the interview is the hope that the interview can talk about the use of scenarios, or principles, because only when you know when to use, what will happen when you use, this can be regarded as the understanding of this knowledge point.

Here’s a look at how they can be used and implemented.

Usage scenarios Call and apply
  1. Find a maximum and a minimum
var numbers = [1, 20, 3, 40 ,60]; Math.max.apply(Math, numbers); // 60 Math.min.apply(Math, numbers); // 1 Math.max.call(Math, ... numbers); // 60 Math.min.call(Math, ... numbers); / / 1Copy the code
  1. Merge array
var arr1 = [1, 2]; var arr2 = [3, 4]; Number combination and the second to the first Array Array. The prototype. Push. Apply (arr1, arr2); // 4 console.log(arr1); // [1, 2, 3, 4]Copy the code
  1. Verify array
 function isArray(obj) {
 	return Object.prototype.toString.call(obj) === '[object Array]';
 }
 
 var number = isArray(1); // false
 var obj = isArray({a:1}); // false
 var string = isArray('1'); // false
 var boolean = isArray(true); // false
 var array = isArray([1]); // true
Copy the code
  1. Call the parent constructor to implement inheritance
 function Person(name, age){
 	this.name = name;
 	this.age = age;
 }
 
 function Jack(name, age){
 	Person.call(this, name, age);
 }
 
 function Peter(name, age){
 	Person.call(this, name, age);
 }
 
 var jack = new Jack('jack'.18); // {name:'jack', age:18}
 var peter = new Peter('peter'.18); // {name:'peter', age:18}By calling call from the parent function, the above two constructors create instances that have the properties of the parent function, thus achieving inheritance.Copy the code
Analog implementation
call
Let's first describe what a call is used for. What do we get1.Multiple arguments are passed, the first of which is the one you want to changethisPoint when not to pointwindow
2.changethisPoint to the3.The function that calls call is executed and can pass in arguments4.Returns the execution result of the function that called callFunction.prototype.myCall = function (context, ... args) {
	Object(context) because this can be passed to the base data type. Native Calls are automatically converted with Object()
	context = context ? Object(context) : Window;
	// Change this pointer
	context.fn = this;
	// Execute the function, passing in the argument (here is the array deconstruction of es6)
	letresult = context.fn(... args);// Delete attribute after function execution
	delete context.fn;
	// Returns the result of the function execution
	returnresult; }; Try using the example above:function Person(name, age){
	this.name = name;
	this.age = age;
}

function Jack(name, age){
	Person.myCall(this, name,age);
}

function Peter(name, age){
	Person.myCall(this, name, age);
}

var jack = new Jack('jack1'.18); // {name:'jack1', age:18}
var peter = new Peter('peter2'.18); // {name:'peter2', age:18}

--------------------------------------------------------------------
function isArray(obj) {
	return Object.prototype.toString.myCall(obj) === '[object Array]';
}

var number =  isArray(1); // false
var obj = isArray({a:1}); // false
var string = isArray('1'); // false
var boolean = isArray(true); // false
var array = isArray([1]); // true
Copy the code
apply
The implementation of Apply simply modifies the parameters receivedFunction.prototype.myApply = function (context, args) {
	Object(context) because this can be passed to the base data type. Native Apply automatically converts this to Object()
	context = context ? Object(context) : Window;
	// Change this pointer
	context.fn = this;
	// Execute the function, passing in the argument (here is the array deconstruction of es6)
	letresult = context.fn(... args);// Delete attribute after function execution
	delete context.fn;
	// Returns the result of the function execution
	returnresult; }; Try using the example above:var arr1 = [1.2];
var arr2 = [3.4]; Combine the second number into the first arrayArray.prototype.push.myApply(arr1,arr2); / / 4
console.log(arr1); // [1, 2, 3, 4]

-----------------------------------------------------
var numbers = [1.20.3.40 ,60];

Math.max.myApply(Math, numbers); / / 60
Math.min.myApply(Math, numbers); / / 1


Copy the code