Aviel Fedida
Aviel Fedida

Reputation: 4102

Javascript, Window context

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.

Upvotes: 0

Views: 6864

Answers (3)

Paul Way
Paul Way

Reputation: 1951

"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.org/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.com/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 comments.

Upvotes: 1

c-smile
c-smile

Reputation: 27470

Let'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:

  1. Create window namespace object:

    var window = {}; window.window = window;

  2. Set it prototype to the Window "class"

    window.__proto__ = Window;

  3. Eval the code in context of the window as global namespace object:

    internalEval("content of that script block", window);

  4. 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
});

Upvotes: 3

poke
poke

Reputation: 387915

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.

Upvotes: 2

Related Questions