What is the difference between window and this.window, in javascript?
I am considering a tricky bit of JavaScript that does all kinds of runtime / dynamic manipulation of objects and scripts. In the process, I learn all sorts of little things about Javascript and its features.
However, I'm all at a dead end. Is there ever a situation wherewindow !== this.window; //true
In other words, when will you ever write this.window instead of the direct window?
source to share
"Is there ever a situation when
window !== this.window; //true
"
Of course, whenever it this
refers to an object with no property .window
or with a property .window
that does not point to a global object (assuming the browser environment window
is where it is global).
"... when do you ever write
this.window
instead of directwindow
?
When this
referring to an object that assumes a property .window
that is not a reference to the global.
this
implicitly looked up names in javascript like in C ++ and java.
in javascript by referencing something foo
instead of this.foo
or someobject.foo
trying, in order, any var
-declared variables, then the global namespace.
this.var
only looks at members this
(and its prototype, prototype prototype, etc.)
at the top level in the script, this
refers to the default global namespace, which is the object window
, and where you are until you call the method with foo.meth()
. window
contains, oddly enough, a member called window
that points to itself. So, you can go:
window.window.window.window.location = "some_url"
if you want to. There are actually so many names that you assume "just there" are members of a global object, for example.
Object === window.Object
If you like you can say
var window;
to declare a named variable window
that hides the global window inside that block of code. But don't do this.
I believe you can actually change what the global namespace is, but I don't remember how.
source to share
Well, this is a duplicate but this
refers to the global scope when you are in the global scope (err) and the global scope is window
. (therefore this == window
they window.window == window
are true)
If you use a function that is used as a constructor like ( new Pie()
) is this
no longer global scope, but rather the object that is being instantiated. Which this
really depends on where you are using the code.
source to share