Home > Web Front-end > JS Tutorial > body text

Detailed explanation of this in JavaScript

零下一度
Release: 2017-06-24 14:46:01
Original
1204 people have browsed it

The little goblin in JavaScript——this! !

Regarding this issue, it tortured me all afternoon. I came back to calm down, sort out my thoughts, and summarize the rules.

Of course, this complex issue cannot be explained clearly in one or two sentences. As a rookie, my summary is just to make some "cheating" written test questions, so I can only welcome complaints.


The question is generally divided into two situations

  • The caller is a function

    • The function is called through the object (this points to the object)

    • The function is called alone (this points to undefined, Points to the global in non-strict mode)

  • The caller is the object

    • The object is declared in the function ( this points to undefined, and points to the global in non-strict mode)

    • The object is declared outside the function (this points to the global)

PS: The existence of anonymous functions will cause the loss of this pointer, which is inconsistent with the above situation. Just use bind() to solve it

The above is the detailed content of Detailed explanation of this in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!