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


Groups > comp.lang.javascript > #27921

Re: Expression for the global object

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: Expression for the global object
Date Fri, 29 May 2015 14:57:57 +0100
Organization A noiseless patient Spider
Lines 46
Message-ID <87382fmqoq.fsf@bsb.me.uk> (permalink)
References <JavaScript-20150528222318@ram.dialup.fu-berlin.de> <645d86ba-2299-4371-bb64-90dae6df316a@googlegroups.com> <cspehuFliqhU1@mid.uni-berlin.de> <5567f976$0$12987$c3e8da3$5496439d@news.astraweb.com> <87k2vrmxr4.fsf@bsb.me.uk> <15438118.NDL39TPNyL@PointedEars.de> <55685287$0$13007$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
Injection-Info mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="16012"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18xi31nOLWEFDHxrtuTB8fbuyeay8RK9yQ="
Cancel-Lock sha1:GC5ehDgqwup3MSd95etdE3LXxhA= sha1:mAH4uHHDbl+/xVYH08UE49rsP/Q=
X-BSB-Auth 1.53c59fb6f9512f0077d0.20150529145757BST.87382fmqoq.fsf@bsb.me.uk
Xref csiph.com comp.lang.javascript:27921

Show key headers only | View raw


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.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next 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