Home >Web Front-end >JS Tutorial >Explanation of the functions and differences between apply and call methods in javascript_javascript skills
1. Instructions for call and apply
1. Call and apply are both methods of Function.prototype, which are implemented internally by the JavaScript engine. Because they belong to Function.prototype, so each Function object instance (that is, each method) has call , apply attribute. Since they are attributes of methods, their use is of course specific to methods. These two methods are easily confused because they have the same function, but they are used in different ways.
2, Syntax: foo.call(this, arg1,arg2,arg3) == foo.apply(this, arguments) == this.foo(arg1, arg2, arg3);
3. Similar points: The effects of the two methods are exactly the same.
4. Difference: The parameters passed by the method are different.
2. Example code
1. The example code defines two functions A and B. A contains the flag attribute and tip attribute (this attribute is assigned a function), and B has a flag attribute.
2. Create objects a and b of A and B respectively.
3. The result of running either a.tip.call(b); or a.tip.apply(b); is B.
4. It can be seen from the results that both call and apply can make the B object call the tip method of the A object and modify the current action object of this.