Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Expression for the global object Date: Sat, 30 May 2015 18:56:39 +0200 Organization: PointedEars Software (PES) Lines: 120 Message-ID: <2504653.tFeGh8rt5O@PointedEars.de> References: <645d86ba-2299-4371-bb64-90dae6df316a@googlegroups.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1433005158 6457 eJwNx8kBwCAIBMCWwspylIMo/Zdg5jdcJtauRlMOR5jto5qfn5K/1QezKmfONgPkBl1qIy6BeB6CEQU= (30 May 2015 16:59:18 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Sat, 30 May 2015 16:59:18 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwFwYEBwCAIA7CXECmyc2jR/09Ygp0rdSKRgYe3rqws5LWTEtl+Md9g900UWkszpyuIj2DDJ80hWy/o/AFuMhY0 Cancel-Lock: sha1:nk+taH76VqRXONHelBk0XOhLSMw= X-NNTP-Posting-Host: eJwFwQkBwDAIA0BL5UkAOXQF/xJ2B6PwCyfoWKxhtOL55HnwYKmyeA3Wew6eqUZJi07eZM8P/YIQGw== Xref: csiph.com comp.lang.javascript:27931 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: _______ ¹ 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] -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.