Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the apply () method in jquery

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use the apply () method in jquery". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the apply () method in jquery".

In jquery, the apply () method is used to change the direction of the this and replace the current object with another object, which is a method of applying an object with the syntax "apply (thisobj, [argarray])"; the parameter argarray represents passing as an array.

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 do the same thing: change the this direction.

Specific grammar:

Apply (thisobj, [argarray])

The function of the apply () method is similar to that of the call () method, except that the apply () method has only two parameters, the thisobj parameter is the same as the call () method, and the second parameter, argarray, is passed as an array, which is different from call (). Call () can pass multiple separate parameters in addition to thisobj.

The two methods are slightly different in the organization of parameters, and their functions are similar.

The calling object of the call method is usually a function, and the function itself is a kind of 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 global window.

Examples are as follows:

For example:

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)

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

Add.call, when the thisobj parameter is passed into s, the this in function add points to s, so the output is 6. 0.

Function add (c, d) {return this.a + this.b + c + d;} var o = {a var 1, b v 3}; add.call (o, 5, 7); / / 1 + 3 + 5 + 7 = 16 this points to oadd.apply (o, [10,20]) / / 1 + 3 + 10 + 20 = 34 this points to o Thank you for your reading. The above is the content of "how to use the apply () method in jquery". After the study of this article, I believe you have a deeper understanding of how to use the apply () method in jquery, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report