"Michael Haufe (TNO)" <tno@thenewobjective.com> writes:
>On Thursday, May 28, 2015 at 3:27:31 PM UTC-5, Stefan Ram wrote:
>[...]
>>What is the smallest scope- and host-independent expression
>>whose value is the global object?
>g
>where g defined in a scope without "use strict" as the expression:
>(function(){return this}).call(null)
Thank you! But I deem the value of the expression »g« not to
be scope independent because an inner scope might redefine »g«.
let g = (function(){return this}).call(null);
console.log( g.Math.cos( 0 )+ 117 );
function f(){ "use strict"; let g = 0; console.log( g.Math.cos( 0 )+ 118 ); }
f();
118
TypeError: g.Math is undefined
To explain what I want:
In C++, »std« is anything that has been named »std« in the
current scope. But /in any scope/, »::std« is the global
::std namespace. So to make sure that you refer to it
whereever you are, you can use »::std«.
In JavaScript, when I write »Math«, this refers to anything
that happens to be called so:
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.