Home  >  Article  >  Web Front-end  >  Detailed explanation of this in JavaScript

Detailed explanation of this in JavaScript

零下一度
零下一度Original
2017-06-24 14:46:011186browse

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!

Statement:
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