The role of 1.apply and call
--These two methods can hijack another object's method, inheriting another object's properties.
The difference and usage of 2.apply and call
——
function.apply (Obj,args)//The apply method has two parameters, the first is an object, and the second is an array. Function.call (obj,[param1[,param2[,... [, Paramn] ]])//Unlike the Apply method is the second starting argument, which is the argument list, and the second argument to apply is an array. Example://Applyfunctionpeople (name,age) { This. Name =name; This. Age =Age ;}functionMan (name,age,sex) {people.apply ( This, arguments); This. Sex = "man";}//Pagerfunctionpeople (age,name) { This. Name =name; This. Age =Age ;}functionMan (name,age,sex) {People.call ( This, Age,name,sex); This. Sex = "man";}
Note that in the example of apply, apply is used here because the order of the first two parameters of man and people is the same.
In the call example, since the first two parameter order of people and man is different, the parameter after this here using the Call,call method is where you can specify the corresponding value of the parameter list.
About Apply and call