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


Groups > comp.lang.javascript > #27931

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-30 18:56 +0200
Organization PointedEars Software (PES)
Message-ID <2504653.tFeGh8rt5O@PointedEars.de> (permalink)
References <JavaScript-20150528222318@ram.dialup.fu-berlin.de> <645d86ba-2299-4371-bb64-90dae6df316a@googlegroups.com> <cspehuFliqhU1@mid.uni-berlin.de>

Show all headers | View raw


Stefan Ram wrote:

> function f(){ "use strict"; let Math = 119; console.log( Math + 1 ); }
> f();
> 
> 120
> 
>   . I wonder, what I can write to always refer the the famous
>   global »Math« object? Not only in code, but also in text,
>   like for example:
> 
>       »Math.cos can be used to calculate the cosine.«.
>  
>   The above sentence is not true inside of the function »f« above.
>   I wish to write something that is /always true/.
>   With a means of C++, I'd write:
> 
>       »::Math.cos can be used to calculate the cosine.«.
> 
>   But I don't know how to write an expression that always
>   refers to the global Math object in JavaScript.

As identifiers are resolved at runtime along the scope chain, such an 
expression cannot exist.

However, an expression that refers to the original Math object in most cases 
is a call of a function defined in the global execution context that returns 
the reference.  This would be the least error-prone approach, but rather 
inefficient:

  /* in global context */
  var getMath = (function (originalMath) {
    return function () {
      return originalMath;
    };
  }(Math));

  function … (…)
  {
    var originalMath = getMath();

    // use originalMath
  }

and the like.  Still, “getMath” could be a local symbol referencing a 
callable object that returns something very different.  You can reduce the 
probability that this happens with pseudo-namespaces, and by attempting to 
make properties read-only, but not to zero.

But seriously, I would not use any code that redefined “Math” in a way that 
is incompatible with the built-in object.

Likewise for the global object:

  var getGlobal = (function (global) {
    return function () {
      return global;
    };
  }(this));

I have introduced “jsx.global” for that purpose, alleviating the problem.  
In essence [1]:

  /* in global context */
  var jsx = {
    // …

    global: this
 
    // …
  };

In theory, every time you need access to the global object, the most 
efficient approach, with the shortest effective scope chain, is to use the 
module pattern and pass a reference to it as argument:

(function (global) {

  /* use bar */

}(this));

(Which means that A standalone “g” is unsuited for sure, although “g” is not 
a common variable name, and should probably be replaced by something more 
descriptive, minimizers aside¹; “e”, “i”, “j” and “k” are more likely.)

All that said, there should be a native host-independent way to refer to the 
global object (“window” is not it; also, few are aware of its inefficiency 
as “window” is a property of the global object which makes the effective 
scope chain longer than necessary).  Node.js has this realized with 
“global”, and I think this should be part of the standard, to become 
available in every implementation.  Unfortunately, a section like

  ---------------------------------------------------------------------
  18.1 Value Properties of the Global Object

  […]

  18.1.4 global
 
  The value of `global' is the return value of GetGlobalObject()
  (see 8.3.5).  This property has the attributes { [[Writable]]: false, 
  [[Enumerable]]: false, [[Configurable]]: false }.
  ---------------------------------------------------------------------

did not make it into the Final Draft of ECMA-262 6th Edition of April 2015:

<http://wiki.ecmascript.org/lib/exe/fetch.php?id=harmony%3Aspecification_drafts&cache=cache&media=harmony:ecma-262_6th_edition_final_draft_-04-14-15.pdf>

_______
¹  When writing a minimizer, not only do you need to replace existing 
   symbols with shorter ones, but also you need to take care that the
   replacement name is not already used.

[1] <http://PointedEars.de/wsvn/JSX/trunk/object.js>
-- 
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 — Previous in thread | Find similar | Unroll thread


Thread

Re: Expression for the global object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2015-05-28 13:49 -0700
  Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-28 22:59 +0200
    Re: Expression for the global object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2015-05-29 07:24 -0700
      Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-29 17:24 +0200
        Re: Expression for the global object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2015-05-30 06:07 -0700
          Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-30 15:20 +0200
            Re: Expression for the global object Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-05-30 11:23 -0300
              Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-30 19:05 +0200
  Re: Expression for the global object Steven D'Aprano <steve@pearwood.info> - 2015-05-29 15:30 +1000
    Re: Expression for the global object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-29 12:25 +0100
      Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-29 13:33 +0200
        Re: Expression for the global object Steven D'Aprano <steve@pearwood.info> - 2015-05-29 21:50 +1000
          Re: Expression for the global object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-29 14:57 +0100
            Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-31 16:18 +0200
  Re: Expression for the global object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2015-05-29 07:31 -0700
    Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-30 15:25 +0200
  Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-30 18:56 +0200

csiph-web