Home Web Front-end JS Tutorial What does this point to in a JavaScript function?

What does this point to in a JavaScript function?

Nov 27, 2018 am 10:46 AM
this

Today I will share with you an important knowledge point in JavaScript, this. It has certain reference value and I hope it will be helpful to everyone's learning.

We often feel confused when learning the this keyword. We are not sure what it refers to in the function and how to use it. Today, I will introduce you to the details about this in the article. Knowledge

[Recommended courses:JavaScript Tutorial

Context and this Keyword

In JavaScript, a function has its own execution context. Pay special attention here that the execution context of a function is not about how it is declared, nor about the function of the function, but about How to call it in code, we will use this when calling this execution context. When this is accessed from within a function, its execution context is actually accessed.

The method of calling the function and this

The context depends on the method of calling the function. We can call the function of the context in 4 different ways, so there are also four types of this Different pointers

1. A basic function call

2. Using the context object to call the function, also called implicit binding.

3. Use call() or call the function apply(), also known as explicit binding.

4. Binding through the bind() method

Basic function call

Basic function call is the simplest way to call a function

Example:

<script type="text/javascript">
	var name="张三";
	function student(){
		console.log(this.name);
	}
	student();
</script>
Copy after login

Image 1.jpg

In this example, it can be seen that student() calls the function from the global scope, so this here refers to the global scope, so The output result is ""Zhang San""

Implicit binding

When a function is "contained" by an object, we say that the this of the function is implicitly bound Bound into this object

<script type="text/javascript">
var student={
	name:"张三",
	obj:function(){
		console.log(this.name)
	}
}
student.obj();
	</script>
Copy after login

Image 2.jpg

Although the obj function is placed in the object, it will not be different from the outside just because it is defined inside the object. , under implicit binding, obj can still access the a property in the student object through this

Display binding

You can use call() or apply( ) method calls a function, and its execution context is explicitly bound to the object. call and apply can change this pointer

Example: when call () or apply() is not used

<script type="text/javascript">
var name="张三";
var obj = {
	name:"李四",
	fun:function(){
	console.log(this.name);}}
obj.fun();
</script>
Copy after login

Image 3.jpg

Use call() to make this point to window

<script type="text/javascript">
var name="张三";
var obj = {
name:"李四",
fun:function(){
      onsole.log(this.name);}}
       obj.fun.call(window);
</script>
Copy after login

Image 1.jpg


##bind() binding

bind() will create a function, and the value of this object in the function body will be bound to the value of the first parameter passed into bind(), but bind does not execute the function and only returns one for execution. The function

<script type="text/javascript">
var a = {
	b : function(){
		var func = function(){
			console.log(this.c);
		}
		func.bind(this)();
	},
	c : &#39;Hello!&#39;
}
a.b();
	</script>
Copy after login


points this to the content in the c object through the bind method.

Image 4.jpg

Summary: The above is the entire content of this article. From the above article we can see that whoever calls this points to who; globally this points to the window ; And the call(), apply() and bind() methods can change the pointer of this.



The above is the detailed content of What does this point to in a JavaScript function?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An article that understands this point and catches up with 70% of front-end people An article that understands this point and catches up with 70% of front-end people Sep 06, 2022 pm 05:03 PM

A colleague got stuck due to a bug pointed by this. Vue2’s this pointing problem caused an arrow function to be used, resulting in the inability to get the corresponding props. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. Today I will share with you this link. If everything is wrong If you haven’t learned it yet, please give me a big mouth.

Let's talk about why Vue2 can access properties in various options through this Let's talk about why Vue2 can access properties in various options through this Dec 08, 2022 pm 08:22 PM

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!

Clever way to use this keyword in jQuery Clever way to use this keyword in jQuery Feb 25, 2024 pm 04:09 PM

Flexible use of this keyword in jQuery In jQuery, the this keyword is a very important and flexible concept. It is used to refer to the DOM element currently being manipulated. By rationally using this keyword, we can easily operate elements on the page and achieve various interactive effects and functions. This article will combine specific code examples to introduce the flexible use of this keyword in jQuery. Simple this example First, let's look at a simple this example. Suppose we have a

What is this? An in-depth analysis of this in JavaScript What is this? An in-depth analysis of this in JavaScript Aug 04, 2022 pm 05:02 PM

What is this? The following article will introduce you to this in JavaScript, and talk about the differences between this in different calling methods of functions. I hope it will be helpful to you!

How to use this method in Java How to use this method in Java Apr 18, 2023 pm 01:58 PM

1. this keyword 1. Type of this: Which object is called is the reference type of that object 2. Usage summary 1. this.data;//Access attribute 2. this.func();//Access method 3.this( );//Call other constructors in this class 3. Explanation of usage 1.this.data is used in member methods. Let us see what will happen if this is not added classMyDate{publicintyear;publicintmonth;publicintday; publicvoidsetDate(intyear,intmonth,intday){ye

How does JavaScript change this pointer? Brief analysis of three methods How does JavaScript change this pointer? Brief analysis of three methods Sep 19, 2022 am 09:57 AM

How does JavaScript change this pointer? The following article will introduce to you three methods of changing this pointer in JS. I hope it will be helpful to you!

Detailed explanation of this in JavaScript arrow function Detailed explanation of this in JavaScript arrow function Jan 25, 2024 pm 01:41 PM

The arrow function in JavaScript is a relatively new syntax. It does not have its own this keyword. On the contrary, the this of the arrow function points to the scope object containing it. The impacts are: 1. This in the arrow function is static; 2. Arrow Functions cannot be used as constructors; 3. Arrow functions cannot be used as methods.

Let me explain in detail the four binding rules of this Let me explain in detail the four binding rules of this Nov 01, 2022 pm 05:49 PM

The this keyword is one of the most complex mechanisms in JavaScript. It is a very special keyword that is automatically defined in the scope of all functions. But even very experienced JavaScript developers have a hard time telling what exactly it points to.

See all articles