This variable is an elusive keyword in JavaScript. This can be said to be very powerful. Fully understanding the relevant knowledge of this will help us be able to write object-oriented JavaScript programs with ease.
The most important thing about this variable is to be able to clarify which object this refers to. Maybe a lot of information has its own explanation, but some concepts are a bit complicated. My understanding is: first analyze which object the function in which this is located is called as a method, then the object is the object referenced by this.
Example 1,
var obj = {}; obj.x = 100; obj.y = function(){ alert( this.x ); }; obj.y(); //弹出 100
This code is very easy to understand. When obj.y() is executed, the function is called as a method of the object obj, so this in the function body points to the obj object. So 100 will pop up.
Example 2,
var checkThis = function(){ alert( this.x); }; var x = 'this is a property of window'; var obj = {}; obj.x = 100; obj.y = function(){ alert( this.x ); }; obj.y(); //弹出 100 checkThis(); //弹出 'this is a property of window'
Why 'this is a property of window' pops up here may be a bit confusing. There is a rule in JavaScript variable scope that "global variables are properties of the window object." When checkThis() is executed, it is equivalent to window.checkThis(). Therefore, at this time, the point of the this keyword in the checkThis function body becomes the window object, and because the window object has another x attribute ('thisis a property of window' ), so 'this is a property of window' will pop up.
The two examples above are relatively easy to understand, because as long as you determine which object the current function is called as a method (which object it is called by), you can easily determine the point of the current this variable.
this.x and apply(), call()
Through call and apply, you can redefine the execution environment of the function, that is, the point of this, which is very common in some applications.
Example 3: call()
function changeStyle( type , value ){ this.style[ type ] = value; } var one = document.getElementByIdx( 'one' ); changeStyle.call( one , 'fontSize' , '100px' ); changeStyle('fontSize' , '300px'); //出现错误,因为此时changeStyle中this引用的是window对象,而window并无style属性。
Note that there are three parameters in changeStyle.call(). The first parameter is used to specify which object the function will be called. One is specified here, which means that the changeStyle function will be called by one, so this point in the function body is the one object. The second and third parameters correspond to the two formal parameters type and value in the changeStyle function. The most common effect we see is that the font of Dom element one becomes 20px.
Example 4: apply()
function changeStyle( type , value ){ this.style[ type ] = value; } var one = document.getElementByIdx( 'one' ); changeStyle.apply( one , ['fontSize' , '100px' ]); changeStyle('fontSize' , '300px'); //出现错误,原因同示例三
The usage of apply is roughly the same as that of call. There is only one difference. apply only accepts two parameters. The first parameter is the same as call. The first parameter is the same as that of call. The two parameters must be an array, and the elements in the array correspond to the formal parameters of the function.
This in the event listening function
var one = document.getElementByIdx( 'one' ); one.onclick = function(){ alert( this.innerHTML ); //this指向的是one元素,这点十分简单.. };
Note: Global variables in js will be dynamically added to the instance of Window as its attribute.
The above is the detailed content of How is this variable used in javascript?. For more information, please follow other related articles on the PHP Chinese website!

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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),
