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


Groups > comp.lang.javascript > #27939

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-31 15:08 +0200
Organization PointedEars Software (PES)
Message-ID <5452925.Zt9fTqAOub@PointedEars.de> (permalink)
References <JavaScript-20150528222318@ram.dialup.fu-berlin.de> <Function-20150531094321@ram.dialup.fu-berlin.de> <87twuthx5h.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse wrote:

> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>> What is the smallest scope- and host-independent expression
>>> whose value is the global object?
>> Function("return this")()
> 
> How does that help?  The identifier Function is looked-up like any
> other.

My test in Chromium 43¹ shows that this approach helps insofar that, 
different from the code of functions defined with a /FunctionDeclaration/ or 
/FunctionExpression/, this function code is only “strict function code […] 
if the function code begins with a Directive Prologue that contains a Use 
Strict Directive” [1][2]:

  Function('"use strict"; …')();

So if “Function” refers to the Function constructor, that expression – 
regardless whether the constructor is called as a function, without “new” – 
returns a reference to the global object, regardless whether the calling 
execution context is in strict mode.  (I stand corrected.)

However, it should be noted that not only local declarations can override 
“Function” as per identifier resolution (as you pointed out), but also that 
“Function” is one of the “constructor properties” of the global object for 
which no different property attributes are specified, meaning that it has 
the “attributes {[[Writable]]: true, [[Enumerable]]: false, 
[[Configurable]]: true}.” – it can be *redefined*. [3][4]  This is confirmed 
to be implemented so by

| >>> Object.getOwnPropertyDescriptor(Function("return this")(), "Function")
| Object {writable: true, enumerable: false, configurable: true}

in the same (default) environment.

The “Function” constructor is supported since JavaScript 1.1, JScript 1.0, 
and the first versions of all other major implementations. [5]

This leaves a low probability that it does not do what it is intended to do 
here, but still one greater than zero.  It is difficult to assess which 
probability would be greater: that someone fudges with “Function”, or that 
one’s code is not executed in the global context.  I think that, ultimately, 
one must defer to the fact that social problems cannot be solved completely 
using technology.

___________
¹  “Version 43.0.2357.65 Built on Debian stretch/sid, running on Debian 
   stretch/sid (64-bit)”,
   navigator.userAgent === "Mozilla/5.0 (X11; Linux x86_64) 
   AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.65 Safari/537.36"

[1] <http://ecma-international.org/ecma-262/5.1/#sec-10.1.1>
[2] <http://ecma-international.org/ecma-262/5.1/#sec-15.3.1>
[3] <http://ecma-international.org/ecma-262/5.1/#sec-15.1>
[4] <http://ecma-international.org/ecma-262/5.1/#sec-15.1.4>
[5] <http://PointedEars.de/es-matrix/?filter=^Function>

-- 
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 | Next in thread | Find similar | Unroll thread


Thread

Re: Expression for the global object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-31 11:14 +0100
  Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-31 15:08 +0200
    Re: Expression for the global object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-31 14:56 +0100
      Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-31 16:12 +0200
        Re: Expression for the global object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-31 15:57 +0100
          Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-31 17:33 +0200
            Re: Expression for the global object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-31 16:41 +0100
      Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-31 16:21 +0200

csiph-web