Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: Expression for the global object Date: Sun, 31 May 2015 14:56:30 +0100 Organization: A noiseless patient Spider Lines: 45 Message-ID: <87382cj1f5.fsf@bsb.me.uk> References: <87twuthx5h.fsf@bsb.me.uk> <5452925.Zt9fTqAOub@PointedEars.de> 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="10174"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+uTRHunvSGe7P69DLSPBPskbKI73alnmA=" Cancel-Lock: sha1:ibSgsumF8mC040VflneUyqtkIzM= sha1:lEVYREEVLd7dNrXuoQjmBaM/xbw= X-BSB-Auth: 1.9ccc04145f0849d07834.20150531145630BST.87382cj1f5.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:27940 Thomas 'PointedEars' Lahn writes: > 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.) Oh, did you say otherwise? I suggested using Function when you pointed out the flaw in using thunk that returns this (my first suggestion), but you didn't reply to that post (as far as I can recall). You did say it could not be done, but that is probably true. Your argument was based on the treatment of identifiers, so another argument is needed to show that there is no way to do it without any. I can think of other ways to do it[1], but I can't think of one that does not use some identifier or other. [1] For example, an indirect call to eval also works: (function () { return eval; })()("this") but, again, not for Stefan's purpose. -- Ben.