


What does this in js events represent? Detailed explanation of the usage of this in js (with usage examples)
This article will use examples to analyze the usage of this in js. The understanding of this in js is also explained in detail. From the following example, we can know: for obj.foo(), foo runs in obj environment, so this points to obj; for foo(), foo runs in the global environment, so this points to the global environment. Therefore, the operating results of the two are different. It can be seen that this plays a decisive role in it. I hope this article can give everyone a reference value.
1. The origin of the problem
One sign of learning JavaScript language is to understand the following two writing methods, which may have different results.
var obj = { foo: function () {} }; var foo = obj.foo; // 写法一 obj.foo() // 写法二 foo()
In the above code, although obj.foo
and foo
point to the same function, the execution results may be different. Please see the example below.
var obj = { foo: function () { console.log(this.bar) }, bar: 1 }; var foo = obj.foo; var bar = 2; obj.foo() // 1 foo() // 2
The reason for this difference lies in the use of the this
keyword inside the function body. Many textbooks will tell you that this
refers to the environment in which the function is run. For obj.foo()
, foo
runs in the obj
environment, so this
points to obj
; For foo()
, foo
runs in the global environment, so this
points to the global environment. Therefore, the operating results of the two are different.
This explanation is correct, but textbooks often don’t tell you why this is the case? In other words, how is the running environment of the function determined? For example, why obj.foo()
is executed in the obj
environment, and once var foo = obj.foo
, foo()
Will it be executed in the global environment?
This article will explain the principle of JavaScript processing. Once you understand this, you will fully understand the role of this
.
2. Memory data structure
The reason why JavaScript language has this
design is related to the data structure in memory.
var obj = { foo: 5 };
The above code assigns an object to the variable obj
. The JavaScript engine will first generate an object { foo: 5 }
in the memory, and then assign the memory address of this object to the variable obj
.
In other words, the variable obj
is an address (reference). If obj.foo
is to be read later, the engine first gets the memory address from obj
, then reads the original object from the address and returns its foo
Attributes.
The original object is saved in a dictionary structure, and each attribute name corresponds to an attribute description object. For example, the foo
attribute in the above example is actually saved in the following form.
{ foo: { [[value]]: 5 [[writable]]: true [[enumerable]]: true [[configurable]]: true } }
Note that the value of the foo
attribute is stored in the value
attribute of the attribute description object.
3. Function
This structure is very clear. The problem is that the value of the attribute may be a function.
var obj = { foo: function () {} };
At this time, the engine will save the function separately in the memory, and then assign the address of the function to the value
of the foo
attribute Attributes.
{ foo: { [[value]]: 函数的地址 ... } }
Since the function is a single value, it can be executed in different environments (contexts).
var f = function () {}; var obj = { f: f }; // 单独执行 f() // obj 环境执行 obj.f()
4. Environment variables
JavaScript allows other variables of the current environment to be referenced within the function body.
var f = function () { console.log(x); };
In the above code, the variable x
is used in the function body. This variable is provided by the runtime environment.
Now comes the problem. Since functions can be executed in different running environments, there needs to be a mechanism to obtain the current running environment (context) inside the function body. Therefore, this
appears. Its design purpose is to refer to the current running environment of the function inside the function body.
var f = function () { console.log(this.x); }
In the above code, this.x
in the function body refers to x
of the current running environment.
var f = function () { console.log(this.x); } var x = 1; var obj = { f: f, x: 2, }; // 单独执行 f() // 1 // obj 环境执行 obj.f() // 2
In the above code, function f
is executed in the global environment, and this.x
points to x
of the global environment.
is executed in the obj
environment, this.x
points to obj.x
.
Back to the question raised at the beginning of this article, obj.foo()
finds foo
through obj
, so it is in obj
environment execution. Once var foo = obj.foo
, the variable foo
points directly to the function itself, so foo()
becomes executed in the global environment.
Related recommendations:
The above is the detailed content of What does this in js events represent? Detailed explanation of the usage of this in js (with usage examples). For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
