Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use javascript apply

Shulou Source: shulou.com Published: 2022-06-02 20:55:34 10月25日 Update

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!

Tags: Methods parameters different functions arrays functions contents objects forms learning practical deeper similar same upper and lower context between interests examples practicality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft vpn MySQL Shulou Information MariaDB