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


Groups > comp.lang.javascript > #27914

Re: Expression for the global object

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.datemas.de!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Expression for the global object
Date Thu, 28 May 2015 22:51:41 +0200
Organization PointedEars Software (PES)
Lines 53
Message-ID <1525105.lGDWB4E9GZ@PointedEars.de> (permalink)
References <JavaScript-20150528222318@ram.dialup.fu-berlin.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace solani.org 1432846461 12728 eJwFwYEBwCAIA7CXrNBunCPD/n/CEoag70lRSdOwWLd4tivPbN3mVPpFoITowYrT7VUaon8ZhRED (28 May 2015 20:54:21 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Thu, 28 May 2015 20:54:21 +0000 (UTC)
User-Agent KNode/4.14.2
X-User-ID eJwFwYEBwDAEBMCVSDw1DuL3H6F3uK4+YQ43EFRxbkMiqlLAeZ9eWzsVzBMfsx+ew8RCh8I2W2wle+YQP1J/Ffw=
Cancel-Lock sha1:bj2pjCnhKTaEWfBJR1f8qZs2R0M=
X-NNTP-Posting-Host eJwNysEBwCAIA8CVSAqxjCNY9h/B3vviEdTLFfKYmBxwLMuSkJMBRx3b/Y+y+FrEfvsUICuuCwNOEE0=
Xref csiph.com comp.lang.javascript:27914

Show key headers only | 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