Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #27914

Re: Expression for the global object

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Expression for the global object
Date 2015-05-28 22:51 +0200
Organization PointedEars Software (PES)
Message-ID <1525105.lGDWB4E9GZ@PointedEars.de> (permalink)
References <JavaScript-20150528222318@ram.dialup.fu-berlin.de>

Show all headers | View raw


Stefan Ram wrote:

>   In JavaScript, there is an object that is called »the global object«.

In all ECMAScript implementations, not just those that now bear the 
“JavaScript” name.

>   In the outermost scope, the global object is the value of
>   the expression »this«. But in special scopes, the expression
>   »this« might have another meaning.

Correct.

>   In web browsers, the global object is called »window«.

No.  The object referred to by “window” has properties that the ECMAScript 
global object does not have.

>   But JavaScript sometimes is used within other hosts than
>   web browsers.
> 
>   What is the smallest scope- and host-independent expression
>   whose value is the global object?

You need to declare a global variable or define a globally available 
property, and assign to it the “this” value in the global execution context.  
For example (as for the former approach),

  var global = this;

Then you can use “global” in any context.  Another possibility is to use the 
module pattern, so that the reference to the global object becomes available 
as the value of a formal parameter:

  (function (global) {
    /* use global here */
  }(this));

Since the module pattern is useful in keeping the global namespace clean, 
this is the most common approach in ECMAScript-based libraries.  The module 
pattern is not “host-independent”, though, because function expressions were 
a later addition to the languages, and need not be supported by all 
ECMAScript implementations (although this is unlikely nowadays; see 
<http://PointedEars.de/es-matrix?filter=function>).

We have discussed this before.  You would be well-advised to broaden and 
deepen your research before you post.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next | Find similar | Unroll thread


Thread

Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-28 22:51 +0200

csiph-web