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 this keyword of JavaScript

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

Share

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

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

What is this?

The JavaScript this keyword refers to the object to which it belongs.

It has different values, depending on where it is used:

In a method, this refers to the owner object.

In individual cases, this refers to a global object.

In a function, this refers to a global object.

In the function, in strict mode, this is undefined.

In an event, this refers to the element that receives the event.

Methods like call () and apply () can reference this to any object.

This in the method

In an object method, this refers to the "owner" of this method.

In the top example on this page, this refers to the person object.

The person object is the owner of the fullName method.

FullName: function () {

Return this.firstName + "" + this.lastName

}

Separate this

When used alone, the owner is a global object, so this refers to a global object.

In the browser window, the global object is [object Window]:

Example

Var x = this

This in function (default)

In the JavaScript function, the owner of the function binds this by default.

Therefore, in the function, this refers to the global object [object Window].

Example

Function myFunction () {

Return this

}

At this point, I believe you have a deeper understanding of "how to use the this keywords of JavaScript". 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