1. Call and Apply description
1. Call, Apply is a method of Function.prototype. It is implemented in the JavaScript engine because it belongs to the function.prototype, so each Function object (that is each method) has a call and Apply attribute. Since the attributes of the method, they are of course targeting the method. These two methods are easy to confuse, because their role is the same, only the way of use is different.
2. Grammar: FOO.CALL (this, ARG1, ARG2, ARG3) == Foo.apply (this, ARGUMENTS) == this.foo (arg1, arg2, arg3);
3. The same point: The function of the two methods is exactly the same.
4. Different points: Different parameters of the method pass.
2. Example code
Copy code code as follows:
<script type = "text/javascript">
function a () {
this.flag = 'a';
this.tip = Function () {{
Alert (this.flag);
};
}
function b () {
this.flag = 'B';
}
var a = new a ();
var b = new b ();
//a.tip.call (B);
a.tip.apply (b);
</script>
Third, code explanation (that is, the role of Apply and Call)
1. The instance code defines two functions A and B. A contains FLAG properties and TIP attributes (this attribute is assigned a function), and B has a flag attribute.
2. Create an object A and B of A and B, respectively.
3. Whether it is A.TIP.Call (b); and a.tip.apply (b); the results of the running are B.
4. From the results, we can see that Call and Apply can allow the T method of the A object to call the A object, and modify the current object of this.