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 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!