The first section: a simple example
function Add (A/b) { Console.log (a +B); } function Sub (b) { console.log (A-b);} add.call (sub,3,2);//5
Summary: The above code defines two methods, and in the last word, it may be understood that a sub object calls the method of the Add object and takes the parameters into the method.
section II: In-depth understanding of call and apply
The definition of call and apply is a non-inheriting method of a function that calls a function within a specific scope, sets the value of the This object in the function body,
function Njie () { this.name = "Njie", function() {Console.log (thisfunction this.name = "KK"newNew KK (); N.sayname ();//njie N.sayname.call (k);//kk
Summary: For example: K object does not have Sayname function, and n has this function, that k by call to borrow N sayname use, of course, also to the value in K into the method.
Section III: The difference between call and apply
Description: The difference between call and apply is that the two incoming parameters are different in the same way
//Parent classfunctionPerson (name,age) {THIS.name =Namethis.age = Age;} // sub-class student function Student (Name,age,grade) {person.apply (this,arguments); //// Person.call (this,name,age); Of course, you can also use the call method this.grade = grade;} // Create an instance of a student var S1 =
Summarize:
The above example has two points:
1, it is possible to use the call method to do the inheritance of JS processing;
The difference between 2,call and apply is mainly in the following parameters, call parameters to list the parameters, and apply can use arguments this property directly with the past.
The call and apply in JS