Home > Web Front-end > JS Tutorial > Analysis of this example in JavaScript_javascript skills

Analysis of this example in JavaScript_javascript skills

WBOY
Release: 2016-05-16 18:07:12
Original
1328 people have browsed it

It seems that this sentence is very reasonable if you use people as a mirror to know the gains and losses.

Demo 1:
If it is a global function, this is equivalent to the window object. Various properties or methods defined in the function can be accessed outside the function, provided that this function needs to be called. .

Copy code The code is as follows:



Demo 2:
If you use new to instantiate an object, this is not equal to the window object, this points to the instance of function a
Copy the code The code is as follows:



Demo 4:
Whether it is directly referencing a function or instantiating a function, the closure returned This in the function all points to window
Copy code The code is as follows:



Demo 5:
in This is used in HTML and generally represents the element itself
Copy code The code is as follows:

< div onclick="test(this)" id="div">Click Me



Demo 6:
Register events under IE and Firefox (Chrome), this points to the window and the element itself respectively
Copy code The code is as follows:

Click Me< /div>


The above is my summary of the different application scenarios of this in javascript. There may be other situations, and I will add them if I find them later.
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template