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


Groups > comp.lang.javascript > #27913 > unrolled thread

Re: Expression for the global object

Started by"Michael Haufe (TNO)" <tno@thenewobjective.com>
First post2015-05-28 13:49 -0700
Last post2015-05-30 18:56 +0200
Articles 17 — 5 participants

Back to article view | Back to comp.lang.javascript

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#27913 — Re: Expression for the global object

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2015-05-28 13:49 -0700
SubjectRe: Expression for the global object
Message-ID<645d86ba-2299-4371-bb64-90dae6df316a@googlegroups.com>
On Thursday, May 28, 2015 at 3:27:31 PM UTC-5, Stefan Ram wrote:

[...]

>   What is the smallest scope- and host-independent expression
>   whose value is the global object?


g

where g defined in a scope without "use strict" as the expression:

(function(){return this}).call(null)

[toc] | [next] | [standalone]


#27915

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-28 22:59 +0200
Message-ID<1442639.SsZ0RATE5U@PointedEars.de>
In reply to#27913
Michael Haufe (TNO) wrote:

> On Thursday, May 28, 2015 at 3:27:31 PM UTC-5, Stefan Ram wrote:
> [...]
>>   What is the smallest scope- and host-independent expression
>>   whose value is the global object?
> 
> g
> 
> where g defined in a scope without "use strict" as the expression:
> 
> (function(){return this}).call(null)

This is not going to work.  Strict mode is hereditary.

-- 
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.

[toc] | [prev] | [next] | [standalone]


#27922

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2015-05-29 07:24 -0700
Message-ID<79b520ed-6efe-4064-a9cf-ea66c60f00df@googlegroups.com>
In reply to#27915
On Thursday, May 28, 2015 at 4:02:23 PM UTC-5, Thomas 'PointedEars' Lahn wrote:
> Michael Haufe (TNO) wrote:
> 
> > On Thursday, May 28, 2015 at 3:27:31 PM UTC-5, Stefan Ram wrote:
> > [...]
> >>   What is the smallest scope- and host-independent expression
> >>   whose value is the global object?
> > 
> > g
> > 
> > where g defined in a scope without "use strict" as the expression:
> > 
> > (function(){return this}).call(null)
> 
> This is not going to work.  Strict mode is hereditary.

You are correct that it is hereditary, but I am correct (if not over-general) when I said: 'a scope without "use strict"'. I left out the work "lexical' on purpose. 

I'll take it as valid criticism that I should have been more specific on this point

[toc] | [prev] | [next] | [standalone]


#27924

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-29 17:24 +0200
Message-ID<12824458.XGgQv3ng7H@PointedEars.de>
In reply to#27922
Michael Haufe (TNO) wrote:

> […] Thomas 'PointedEars' Lahnwrote:
>> Michael Haufe (TNO) wrote: 
>> > On Thursday, May 28, 2015 at 3:27:31 PM UTC-5, Stefan Ram wrote:
>> > [...]
>> >>   What is the smallest scope- and host-independent expression
>> >>   whose value is the global object?
>> > 
>> > g
>> > 
>> > where g defined in a scope without "use strict" as the expression:
>> > 
>> > (function(){return this}).call(null)
>> 
>> This is not going to work.  Strict mode is hereditary.
> 
> You are correct that it is hereditary, but I am correct (if not
> over-general) when I said: 'a scope without "use strict"'. I left out the
> work "lexical' on purpose.

There is only lexical scoping in ECMAScript implementations, so what is 
omitting “lexical” supposed to change?

,-<http://ecma-international.org/ecma-262/5.1/#sec-8.6.2>
| 
| Internal Property    Value Type Domain      Description
| […]
| [[Scope]]            Lexical Environment    A lexical environment that
|                                             defines the environment in
|                                             which a Function object is 
|                                             executed. Of the standard 
|                                             built-in ECMAScript objects, 
|                                             only Function objects 
|                                             implement [[Scope]].

Consider this code:

  "use strict";

  (function () {
    var g = (function(){return this}).call(null);

    /* "null" */
    console.log(g);
  }());

The (lexical) scope in which “g” is defined as required per your suggestion 
is that of the function.  This scope is obviously “without "use strict"”.  
But it still does not work (“g” holds “null”, not a reference to the global 
object).  Conclusion: Your statement is wrong, and you are _not_ correct.

It is possible that you have been mislead by the Chrome Dev Console.  If you 
write

  "use strict"; // …

the strict declaration has no effect.  This has to do with how statements 
are evaluated by the Console.  You need to write

  (function () {
    "use strict";

    // …
  }());

to test strict mode there.

-- 
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.

[toc] | [prev] | [next] | [standalone]


#27925

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2015-05-30 06:07 -0700
Message-ID<36af7f41-d441-48a2-81a1-28e16e063e54@googlegroups.com>
In reply to#27924
On Friday, May 29, 2015 at 10:27:37 AM UTC-5, Thomas 'PointedEars' Lahn wrote:

> | [[Scope]]

[...]

>[...] This scope is obviously "without "use strict"".  [...]

We need to be clear and consistent in terminology then. "scope" vs "[[Scope]]"

I am using "scope" as it is commonly used: "the textual region of a program in which a binding is active". Thus, if I had the following:

var x = "foo";

(function(){
    var y = "bar"
})();

'x' is still "in scope" inside the body of the function. Following the same vein regarding "use strict";

Now, if you are saying that you were referring to "[[Scope]]" the entire time and not "scope", then I would ask you write it that way and expect you to start hammering people over the head with it the same way you do with "javascript" vs. "JavaScript": "There is no scope in JavaScript, there is only [[Scope]]..."

[toc] | [prev] | [next] | [standalone]


#27927

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-30 15:20 +0200
Message-ID<1568146.gjZlAL6gd7@PointedEars.de>
In reply to#27925
Michael Haufe (TNO) wrote:

> […] Thomas 'PointedEars' Lahn wrote:
>> | [[Scope]]
> 
> [...]
> 
>>[...] This scope is obviously "without "use strict"".  [...]
> 
> We need to be clear and consistent in terminology then. "scope" vs
> "[[Scope]]"

I fail to see a difference.

> I am using "scope" as it is commonly used: "the textual region of a
> program in which a binding is active".

So do I.  However, it is prudent to use a definition from the ECMAScript 
Language Specification when discussing implementations of ECMAScript.

> Thus, if I had the following:
> 
> var x = "foo";
> 
> (function(){
>     var y = "bar"
> })();
> 
> 'x' is still "in scope" inside the body of the function.

Yes.

> Following the same vein regarding "use strict";

"in scope" is a necessary, but not a sufficient criterion.

You can never be sure that your code is only executed in the global context, 
and the global context always can enforce strict mode.  In fact, it is 
reasonable to assume that it is not, because of the side effects and of the 
prevalence of the module pattern.

It is not logical to use approaches that make code less compatible than it 
needs to be.

-- 
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.

[toc] | [prev] | [next] | [standalone]


#27929

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2015-05-30 11:23 -0300
Message-ID<mkch5q$vhp$1@speranza.aioe.org>
In reply to#27927
Thomas Lahn wrote:
> Michael Haufe (TNO) wrote:
>
>> […] Thomas 'PointedEars' Lahn wrote:
>>> | [[Scope]]
>>
>> [...]
>>
>>> [...] This scope is obviously "without "use strict"".  [...]
>>
>> We need to be clear and consistent in terminology then. "scope" vs
>> "[[Scope]]"
>
> I fail to see a difference.
>

>
>> Following the same vein regarding "use strict";
>
> "in scope" is a necessary, but not a sufficient criterion.
>
> You can never be sure that your code is only executed in the global context,
> and the global context always can enforce strict mode.  In fact, it is
> reasonable to assume that it is not, because of the side effects and of the
> prevalence of the module pattern.
>
> It is not logical to use approaches that make code less compatible than it
> needs to be.
>

Mr. Spock states: "Any show of resistance would be futile."

-- 
Joao Rodrigues

[toc] | [prev] | [next] | [standalone]


#27932

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-30 19:05 +0200
Message-ID<1833124.2mcessIEoZ@PointedEars.de>
In reply to#27929
Joao Rodrigues wrote:

> Mr. Spock states: "Any show of resistance would be futile."

Bonus points if you can name the episode or movie, and the scene in which he 
said this.
 
-- 
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.

[toc] | [prev] | [next] | [standalone]


#27917

FromSteven D'Aprano <steve@pearwood.info>
Date2015-05-29 15:30 +1000
Message-ID<5567f976$0$12987$c3e8da3$5496439d@news.astraweb.com>
In reply to#27913
On Fri, 29 May 2015 07:10 am, Stefan Ram wrote:

>   . 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.

I *think* that this is not possible, as names in any namespace may shadow
standard names. "Shadow" or "shadowing" is the terminology used in Python
circles, is it also used here?



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#27918

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-05-29 12:25 +0100
Message-ID<87k2vrmxr4.fsf@bsb.me.uk>
In reply to#27917
Steven D'Aprano <steve@pearwood.info> writes:

> On Fri, 29 May 2015 07:10 am, Stefan Ram wrote:
>
>>   . 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.
>
> I *think* that this is not possible, as names in any namespace may shadow
> standard names. "Shadow" or "shadowing" is the terminology used in Python
> circles, is it also used here?

ECMAScript has a 'this' keyword.  You therefore have a means to refer to
at least one object that can't be hidden by shadowed by a name.  I think
you can get a reference to the global object in any execution context
using the rather convoluted

  (function () { return this; }).call(null)

expression.  There might be some cases where this might be used in code,
but it does not meet Stefan's other criterion as it is far too unwieldy
to use in text.

This works because a null value for 'this' is replaced by the global
object when entering function code (10.4.3)., though the twisty passages
of the standard always leave me a little unsure about corner cases.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#27919

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-29 13:33 +0200
Message-ID<15438118.NDL39TPNyL@PointedEars.de>
In reply to#27918
Ben Bacarisse wrote:

> […]  I think
> you can get a reference to the global object in any execution context
> using the rather convoluted
> 
>   (function () { return this; }).call(null)
> 
> expression.

You are wrong.

> There might be some cases where this might be used in code,

There are.

> but it does not meet Stefan's other criterion as it is far too unwieldy
> to use in text.

Pardon?

> This works because a null value for 'this' is replaced by the global
> object when entering function code (10.4.3)., though the twisty passages
> of the standard always leave me a little unsure about corner cases.

AISB, in strict mode this function returns the “undefined” value.

Strict mode is not a "corner case" anymore.

It is _not_ going to work this way.

-- 
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.

[toc] | [prev] | [next] | [standalone]


#27920

FromSteven D'Aprano <steve@pearwood.info>
Date2015-05-29 21:50 +1000
Message-ID<55685287$0$13007$c3e8da3$5496439d@news.astraweb.com>
In reply to#27919
On Fri, 29 May 2015 09:33 pm, Thomas 'PointedEars' Lahn wrote:

> Ben Bacarisse wrote:
> 
>> […]  I think
>> you can get a reference to the global object in any execution context
>> using the rather convoluted
>> 
>>   (function () { return this; }).call(null)
>> 
>> expression.
> 
> You are wrong.

Care to give an example of where the above does not return the global
object?


>> There might be some cases where this might be used in code,
> 
> There are.
> 
>> but it does not meet Stefan's other criterion as it is far too unwieldy
>> to use in text.
> 
> Pardon?

Stefan wants to write text like:

    "Math.cos can be used to calculate the cosine"


where Math cannot be shadowed or overridden by some other object. It is far
too unwieldy to say instead:

    "(function () { return this; }).call(null).Math.cos can be 
    used to calculate the cosine."


Even if this is technically correct, it is far too unwieldy to use, and it
is misleading as well.


>> This works because a null value for 'this' is replaced by the global
>> object when entering function code (10.4.3)., though the twisty passages
>> of the standard always leave me a little unsure about corner cases.
> 
> AISB, in strict mode this function returns the “undefined” value.

It seems to work in Rhino:

[steve@ando ~]$ rhino -strict
Rhino 1.7 release 0.7.r2.3.el5_6 2011 05 04
js> (function () { return this; }).call(null)
[object global]


> Strict mode is not a "corner case" anymore.
> 
> It is _not_ going to work this way.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#27921

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-05-29 14:57 +0100
Message-ID<87382fmqoq.fsf@bsb.me.uk>
In reply to#27920
Steven D'Aprano <steve@pearwood.info> writes:

> On Fri, 29 May 2015 09:33 pm, Thomas 'PointedEars' Lahn wrote:
>
>> Ben Bacarisse wrote:
>> 
>>> […]  I think
>>> you can get a reference to the global object in any execution context
>>> using the rather convoluted
>>> 
>>>   (function () { return this; }).call(null)
>>> 
>>> expression.
<snip>
>>> This works because a null value for 'this' is replaced by the global
>>> object when entering function code (10.4.3)., though the twisty passages
>>> of the standard always leave me a little unsure about corner cases.
>> 
>> AISB, in strict mode this function returns the “undefined” value.

Thanks for pointing that out.  I would offer, instead,

  Function("{return this;}").call(null)

but it does not solve the original problem as the the identifier
'Function' is as unreliable as the original 'Math'.  (The same is true
of my original suggestion in that the call property might have been
changed.)

> It seems to work in Rhino:
>
> [steve@ando ~]$ rhino -strict
> Rhino 1.7 release 0.7.r2.3.el5_6 2011 05 04
> js> (function () { return this; }).call(null)
> [object global]

Yes, but rhino is rather old and, I think, follows version 3 of the
language standard.  The behaviour of a null 'this' changed between
versions and I think this accounts for difference in strict mode.

(Installing nodejs will give you a much more up-to-date ES environment,
and one with very may more library modules.)

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#27943

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-31 16:18 +0200
Message-ID<6342988.ILlWh6QtbD@PointedEars.de>
In reply to#27921
Ben Bacarisse wrote:

>   Function("{return this;}").call(null)

“.call(null)” is equivalent to “()” here.

> but it does not solve the original problem as the the identifier
> 'Function' is as unreliable as the original 'Math'.

That, too.

>> It seems to work in Rhino:
>> […]
> Yes, but rhino […], I think, follows version 3 of the language standard.

It implements _Edition_ 3, see the timeline in the ES Matrix.

> […]
> (Installing nodejs will give you a much more up-to-date ES environment,
> and one with very may more library modules.)

One capable of several ES 6 features by now.

-- 
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.

[toc] | [prev] | [next] | [standalone]


#27923

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2015-05-29 07:31 -0700
Message-ID<34dd9685-0b20-4a75-8283-d8b0319ad126@googlegroups.com>
In reply to#27913
On Thursday, May 28, 2015 at 4:10:28 PM UTC-5, Stefan Ram wrote:
> "Michael Haufe (TNO)" writes:
> >On Thursday, May 28, 2015 at 3:27:31 PM UTC-5, Stefan Ram wrote:
> >[...]
> >>What is the smallest scope- and host-independent expression
> >>whose value is the global object?
>
> >g
>
> >where g defined in a scope without "use strict" as the expression:
> >(function(){return this}).call(null)
> 
>   Thank you! But I deem the value of the expression »g« not to
>   be scope independent because an inner scope might redefine »g«.

define it as a constant then.

const g = ...

[toc] | [prev] | [next] | [standalone]


#27928

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-30 15:25 +0200
Message-ID<16889332.y9n2pRrfs3@PointedEars.de>
In reply to#27923
Michael Haufe (TNO) wrote:

> On Thursday, May 28, 2015 at 4:10:28 PM UTC-5, Stefan Ram wrote:
>> "Michael Haufe (TNO)" writes:
>> >On Thursday, May 28, 2015 at 3:27:31 PM UTC-5, Stefan Ram wrote:
>> >[...]
>> >>What is the smallest scope- and host-independent expression
>> >>whose value is the global object?
>>
>> >g
>>
>> >where g defined in a scope without "use strict" as the expression:
>> >(function(){return this}).call(null)
>> 
>>   Thank you! But I deem the value of the expression »g« not to
>>   be scope independent because an inner scope might redefine »g«.
> 
> define it as a constant then.
> 
> const g = ...

That does not solve the (imagined) problem (that Stefan is looking for for 
his solution).  In fact, it creates another one regarding compatibility, 
contrary to the requirement “host-independent”.

-- 
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.

[toc] | [prev] | [next] | [standalone]


#27931

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-30 18:56 +0200
Message-ID<2504653.tFeGh8rt5O@PointedEars.de>
In reply to#27913
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web