Home > Web Front-end > Front-end Q&A > How to use the apply() method in jquery

How to use the apply() method in jquery

WBOY
Release: 2022-04-24 17:35:32
Original
1990 people have browsed it

In jquery, the apply() method is used to change the this pointer and replace the current object with another object. It is a method of applying a certain object. The syntax is "apply(thisobj,[argarray])"; The parameter argarray indicates that it is passed in the form of an array.

How to use the apply() method in jquery

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

How to use the apply() method in jquery

The call() method and the apply() method have the same effect: change the this pointer.

Specific syntax:

apply(thisobj,[argarray])
Copy after login

The apply() method and the call() method have similar functions. The difference lies in the parameter transfer form. The apply() method has only two parameters. The thisobj parameter is the same as the call() parameter. The usage in the call() method is the same. The second parameter argarray is passed in the form of an array. This is different from call(). In addition to thisobj, call() can pass multiple separate parameters.

The two methods are slightly different in the organization of parameters, but have similar functions.

The calling object of the call method is generally a function, and the function itself is also an object.

The first parameter thisobj is the new context of the function object. Depending on the thisobj object, the execution context of the function may be different. If the thisobj parameter is not passed, the default context is the global window.

Examples are as follows:

For example:

    <script>
 
        var a = 1, b = 1;
 
        function add(a, b)
        {
            alert(this.a + this.b);
        }
 
        var s = {};
        s.a = 5;
        s.b = 1;
 
        add.call();
        //alert(2)
 
        add.call(s, 3, 1);
        //alert(6)
 
    </script>
Copy after login

add.call() does not pass in the thisobj parameter, this in function add points to window, and the output result is 2.

add.call(s,3,1), when the thisobj parameter is passed into s, this in function add points to s, so the output result is 6.

function add(c, d){
    return this.a + this.b + c + d;
}
var o = {a:1, b:3};
add.call(o, 5, 7); // 1 + 3 + 5 + 7 = 16  this指向o
add.apply(o, [10, 20]); // 1 + 3 + 10 + 20 = 34  this指向o
Copy after login

Related video tutorial recommendations: jQuery Video tutorial

The above is the detailed content of How to use the apply() method in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template