Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Why are window and document being fed in instead of just being accessed normally?</p> </blockquote> <p>Generally to fasten the identifier resolution process, having them as local variables can help (although IMO the performance improvements may be negligible).</p> <p>Passing the global object is also a widely used technique on non-browser environments, where you don't have a <code>window</code> identifier at the global scope, e.g.:</p> <pre><code>(function (global) { //.. })(this); // this on the global execution context is // the global object itself </code></pre> <blockquote> <p>Why the heck is undefined being passed in?</p> </blockquote> <p>This is made because the <code>undefined</code> global property in ECMAScript 3, is mutable, meaning that someone could change its value affecting your code, for example:</p> <pre><code>undefined = true; // mutable (function (undefined) { alert(typeof undefined); // "undefined", the local identifier })(); // &lt;-- no value passed, undefined by default </code></pre> <p>If you look carefully <code>undefined</code> is actually not being passed (there's no argument on the function call), that's one of the reliable ways to get the <code>undefined</code> value, without using the property <code>window.undefined</code>.</p> <p>The name <code>undefined</code> in JavaScript doesn't mean anything special, is not a keyword like <code>true</code>, <code>false</code>, etc..., it's just an identifier.</p> <p>Just for the record, in ECMAScript 5, this property was made non-writable...</p> <blockquote> <p>Is attaching the object we're creating directly to window a particularly good idea?</p> </blockquote> <p>It's a common way used to declare global properties when you are on another function scope.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload