Introduction to the Apply () method and the call () method in JavaScript

Source: Internet
Author: User

The role and difference of the apply and call methods in JavaScript explains the description of call and apply
    1. Call,apply is a method of Function.prototype, which is implemented internally by the JavaScript engine because it belongs to Function.prototype, so each function object instance (that is, each method) has a call , apply property. Since as a property of the method, the use of them is of course for the method, and these two methods are easy to confuse, because they work the same way, just different ways of using.
    2. Syntax: Foo.call (this, arg1,arg2,arg3) = = Foo.apply (this, arguments) = = This.foo (arg1, arg2, ARG3);
    3. The same point: two methods produce exactly the same effect.
    4. Different points: The methods pass different parameters.
<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);      b    A.tip.apply (b);       B</script>
code interpretation (i.e., apply and call roles)
    1. The instance code defines two functions A and b,a that contain the flag attribute and the Tip attribute (this property assigns a function), and B has a flag attribute.
    2. Create objects A and B, respectively, for A and B.
    3. Either A.tip.call (b); and a.tip.apply (b); The result of the operation is a popup B.
    4. As you can see from the results, call and apply both allow the B object to invoke the Tip method of the A object and modify the current object of this.
Introduction to the Apply () method and the call () method in JavaScript
    1. Each function contains two non-inherited methods: Apply () and call ().
    2. They are used in the same way, and are called functions in a specific scope.
    3. In terms of receiving parameters, apply () receives two parameters, one is the scope of the function run (this), and the other is the parameter array.
    4. The first parameter of the call () method is the same as the Apply () method, but the arguments passed to the function must be enumerated.

Example 1:

The result of the operation is:

Example 2:

Analysis: in Example 1, we found that the real use of apply () and call () is to be able to expand the scope of the function to run, if we want to implement the traditional method, see the following code:

See Accents's code, we find that to make the Helloname () function scoped on the object MyObject, we need to dynamically create the Helloname property of the MyObject, which points to the Helloname () function as a pointer, so that When we call Myobject.helloname (), the This variable inside the function points to MYOBJECCT, and you can call the internal other public properties of the object.
By analyzing example 2, we can see the real use of the call () and apply () functions, and in the actual project, it is necessary to deal with the actual flexibility!
A small problem: take a look at the case of the this variable when defining a function in a function

//object {} //window

  
The execution results are the same as the following:

Bind () method

Here, Saycolor () calls the Bind () method, passes in the O object, returns the Osaycolor () function, and in Osaycolor (), the value of this is an O object.

Introduction to the Apply () method and the call () method in JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.