Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!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: Fri, 29 May 2015 12:25:19 +0100 Organization: A noiseless patient Spider Lines: 40 Message-ID: <87k2vrmxr4.fsf@bsb.me.uk> References: <645d86ba-2299-4371-bb64-90dae6df316a@googlegroups.com> <5567f976$0$12987$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="14894"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/eg0W+v2kdjqCTnH8iWTb9fVc/IVrZ0oI=" Cancel-Lock: sha1:DQKLGiGCKE6Y8PVbsYIAO32dF9o= sha1:Vn+QTxKXo2caavsOc5baq/A09vE= X-BSB-Auth: 1.f99209cc892261f5127d.20150529122519BST.87k2vrmxr4.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:27918 Steven D'Aprano 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.