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 javascript apply

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use javascript apply". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use javascript apply.

The role of javascript apply is to write methods for different objects; the apply () method accepts parameters in array form, using syntax such as "person.fullName.apply (person1);".

This article operating environment: Windows7 system, javascript1.8.5 version, DELL G3 computer

What is the use of javascript apply?

JavaScript function Apply

Method reuse

With the apply () method, you can write methods for different objects.

JavaScript apply () method

The apply () method is very similar to the call () method:

(the same function: all exist to change the context context in which a function is running, in order to change the direction of the this inside the function body)

In this example, the fullName method of person is applied to person1:

Example

Var person = {fullName: function () {return this.firstName + "" + this.lastName;}} var person1 = {firstName: "Bill", lastName: "Gates",} person.fullName.apply (person1); / / "Bill Gates" will be returned

Note:

The difference between call () and apply ()

The differences are:

The call () method accepts parameters, respectively.

The apply () method accepts parameters as an array.

The apply () method is handy if you want to use arrays instead of parameter lists.

At this point, I believe you have a deeper understanding of "how to use javascript apply". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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