Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why `this===window` gives me false?

Tags:

javascript

I've been testing the following code, but Firefox16 and Chrome22 gives me different outcomes.

console.log(this===window); //false in Firefox and true in Chrome
console.log(this.window===window); //true in both Firefox and Chrome
(function(){
    console.log(this===window); //false in Firefox and true in Chrome
    console.log(this.window===window); //true in both Firefox and Chrome
})();

As far as I can remember, Chrome's answer is right: Unless called with new, this is always same as the global object window, which leads to a pattern called scope safe constructors.

like image 426
Rufus Avatar asked Nov 05 '12 01:11

Rufus


People also ask

How does the this keyword work?

In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called). The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object.

What is this keyword in JavaScript stack overflow?

this is a keyword in JavaScript that is a property of an execution context. Its main use is in functions and constructors.

What is scope of this keyword?

If a function which includes 'this' keyword, is called from the global scope then this will point to the window object. Learn about global and local scope here. In the above example, a function WhoIsThis() is being called from the global scope. The global scope means in the context of window object.

What does this refer to in the global scope of a JavaScript source?

In the global execution context (outside of any function), this refers to the global object whether in strict mode or not. // In web browsers, the window object is also the global object: console. log(this === window); // true a = 37; console. log(window.


1 Answers

It is assumed that window is the global object, but until ES5, there was no specification to define that. It was just convention (i.e. part of "DOM 0"). In ES5 §15.1, there is:

in the HTML document object model the window property of the global object is the global object itself

Which I guess makes it a non–normative part of the ECMAScript standard at least.

For some time, there have been ambiguities between window and the global object, but for most uses, they were synonmuous. It was only certain cases where there were differences.

Regarding the statement:

Unless called with new, this is always same as the global object window

Not at all. A function's this keyword is set by how the function is called. In non–strict mode, if, on entering an execution context, no thisBinding is provided, then it's set to the global object. In strict mode, it is left as is (i.e. it might be undefined, null, 0, anything).

which leads to a pattern called scope safe constructors

I'm not sure what that means. A function's this has nothing to do with scope. When the new operator is used with a function call, then the function acts as a constructor and its this is set to a new object created as if by new Object(). That seems to be more a function of the new operator than a concept of "scope safe constructors".

like image 131
RobG Avatar answered Oct 25 '22 14:10

RobG