Home >Web Front-end >JS Tutorial >A brief discussion on the this pointer in Javascript_javascript skills

A brief discussion on the this pointer in Javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:31:511058browse

(1) Use this in the global execution environment to identify the Global object, which is the window object in the browser.
(2) When using this in a function execution environment, if the function is not obviously an attribute of a non-window object, but just defines a function, regardless of whether the function is defined in another function, in this function The this still identifies the window object. If the function is explicitly used as a property of a non-window object, then this in the function represents this object.

Copy code The code is as follows:

var o=new Object;
o.func =function()
{
alert((this===o));
(function(){
alert((this===window));
}
)();
}
o.func();

(3) When a function is called through the new operator, the function is treated as a constructor, and this points to The object created by the constructor.

For more details, please refer to
Javascript this pointer
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