I try to understand how does the window object wrap my code i will explain, For example i got this theory:
function window(){
// The window.window is in circular structure because window property on
// the window object is set to this (this.window = this)
// All the rest of the developer code goes here
}
window.call({});
This is probably a bad theory, But i wanted to try, My question is how the window object wrap my code how does it looks like, And if i am right about the window.window circular structure?
Edit:
What i want to understand is how is the window object is available to me as my global scope, How does it wrap my code, Is it same or similar to my example?, How the window object is structured behind the scene?
Edit:
Thank you for the answer c-smile, Please just tell me if i got you right,
The internalEval function is a javascript engine function written in java or c++ and so on...?
Also when you say script block you mean all the code between <script></script>
?
When you say "and external script file loaded into the view" you mean that any other <script></script>
tags content founded in the html page will be evaluated against the window scope(global scope)?
And about the window object: The first line that the browser execute before any script is var window = {};
that will represent my global scope, The next step is to build the empty window object by assigning the window.__proto__ = Window;
, Now i belive that the Window (function Window() { [native code] }) is assigned to the global(window) object before this line: window.__proto__ = Window;
for example:
var window = {};
window.Window = function Window() { [native code] }
window.__proto__ = Window;
I really think i got it but please correct me if I'm wrong.
I try to understand how does the window object wrap my code i will explain, For example i got this theory:
function window(){
// The window.window is in circular structure because window property on
// the window object is set to this (this.window = this)
// All the rest of the developer code goes here
}
window.call({});
This is probably a bad theory, But i wanted to try, My question is how the window object wrap my code how does it looks like, And if i am right about the window.window circular structure?
Edit:
What i want to understand is how is the window object is available to me as my global scope, How does it wrap my code, Is it same or similar to my example?, How the window object is structured behind the scene?
Edit:
Thank you for the answer c-smile, Please just tell me if i got you right,
The internalEval function is a javascript engine function written in java or c++ and so on...?
Also when you say script block you mean all the code between <script></script>
?
When you say "and external script file loaded into the view" you mean that any other <script></script>
tags content founded in the html page will be evaluated against the window scope(global scope)?
And about the window object: The first line that the browser execute before any script is var window = {};
that will represent my global scope, The next step is to build the empty window object by assigning the window.__proto__ = Window;
, Now i belive that the Window (function Window() { [native code] }) is assigned to the global(window) object before this line: window.__proto__ = Window;
for example:
var window = {};
window.Window = function Window() { [native code] }
window.__proto__ = Window;
I really think i got it but please correct me if I'm wrong.
Share Improve this question edited Feb 16, 2014 at 18:40 Aviel Fedida asked Feb 16, 2014 at 16:15 Aviel FedidaAviel Fedida 4,1029 gold badges58 silver badges89 bronze badges 9- Why? Please tell us what you are trying to do – mplungjan Commented Feb 16, 2014 at 16:22
- Would you elaborate a bit on what you meant by "how does the window object wrap my code?" window is a global object that DOM exposes so we can access properties of current page user is on. In terms of why window.window exists, you may find this useful: developer.mozilla/en-US/docs/Web/API/Window.window – DashK Commented Feb 16, 2014 at 16:24
- @mplungjan It makes me curious. – Aviel Fedida Commented Feb 16, 2014 at 16:25
- @DashK Thank you for the link, I did edit my answer to explain a bit more about what make me curious, And is the window property is set like this.window = this; inside the window object? – Aviel Fedida Commented Feb 16, 2014 at 16:36
-
1
Yes, but browsers usually aren’t written in JavaScript, so you can’t really explain it with JavaScript code. The global object is part of the spec, and
window
just happens to take that job for browsers. – poke Commented Feb 16, 2014 at 16:48
3 Answers
Reset to default 3Let's say you have some script block on your web page:
<script>
var loc = window.location;
...
</script>
Here is what browser is doing when it needs to run the script in that block:
Create window namespace object:
var window = {}; window.window = window;
Set it prototype to the Window "class"
window.__proto__ = Window;
Eval the code in context of the window as global namespace object:
internalEval("content of that script block", window);
Do #3 for each script block and external script file loaded into the view.
So if your script has statement like this:
someGlobalVar = 1234;
that someGlobalVar will be created as a property of window object:
window.someGlobalVar; // -> 1234 value here
And you cannot create global function with the name window
as that variable
is already defined and configured as read only. So that window.window = window;
statement above actually looks like:
Object.defineProperty(window, "window", {
enumerable: false,
configurable: false,
writable: false,
value: window
});
According to the specification, there needs to be some global object that fulfills certain roles. It provides the global environment, the outmost scope. The global object is something that is created by the JavaScript engine itself, before any JavaScript code actually runs; and you cannot really express how that works in JavaScript code itself.
For browsers executing in the context with a DOM, window
happens to be that global object. Every JavaScript runtime will have one; but what it is depends on where it’s running. For example in Node, the global
object (what a great name, huh?) takes that role.
"window" is created prior to any execution context and cannot be used as the names of a variable or routine in global scope. Here's a great reference @poke linked to: http://www.ecma-international/ecma-262/5.1/#sec-15.1
The window object is used to retrieve information about the window and every frame on a page will have a window object.
Here's some more information http://msdn.microsoft./en-us/library/ie/ms535873(v=vs.85).aspx
Note: There are some inconsistencies in the MSDN article and the EMCAScript. @Poke pointed out in ments.