[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Expression for the global object

ram

5/28/2015 8:27:00 PM

In JavaScript, there is an object that is called »the global object«.

In the outermost scope, the global object is the value of
the expression »this«. But in special scopes, the expression
»this« might have another meaning.

In web browsers, the global object is called »window«.
But JavaScript sometimes is used within other hosts than
web browsers.

What is the smallest scope- and host-independent expression
whose value is the global object?

32 Answers

Michael Haufe (\"TNO\")

5/28/2015 8:49:00 PM

0

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)

Thomas 'PointedEars' Lahn

5/28/2015 8:52:00 PM

0

Stefan Ram wrote:

> In JavaScript, there is an object that is called »the global object«.

In all ECMAScript implementations, not just those that now bear the
â??JavaScriptâ? name.

> In the outermost scope, the global object is the value of
> the expression »this«. But in special scopes, the expression
> »this« might have another meaning.

Correct.

> In web browsers, the global object is called »window«.

No. The object referred to by â??windowâ? has properties that the ECMAScript
global object does not have.

> But JavaScript sometimes is used within other hosts than
> web browsers.
>
> What is the smallest scope- and host-independent expression
> whose value is the global object?

You need to declare a global variable or define a globally available
property, and assign to it the â??thisâ? value in the global execution context.
For example (as for the former approach),

var global = this;

Then you can use â??globalâ? in any context. Another possibility is to use the
module pattern, so that the reference to the global object becomes available
as the value of a formal parameter:

(function (global) {
/* use global here */
}(this));

Since the module pattern is useful in keeping the global namespace clean,
this is the most common approach in ECMAScript-based libraries. The module
pattern is not â??host-independentâ?, though, because function expressions were
a later addition to the languages, and need not be supported by all
ECMAScript implementations (although this is unlikely nowadays; see
<http://PointedEars.de/es-matrix?filter=fu...).

We have discussed this before. You would be well-advised to broaden and
deepen your research before you post.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

5/28/2015 9:00:00 PM

0

Michael Haufe (TNO) wrote:

> 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)

This is not going to work. Strict mode is hereditary.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

ram

5/28/2015 9:10:00 PM

0

"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.

Steven D'Aprano

5/29/2015 5:30:00 AM

0

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?



--
Steven

Ben Bacarisse

5/29/2015 11:25:00 AM

0

Steven D'Aprano <steve@pearwood.info> 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.

Thomas 'PointedEars' Lahn

5/29/2015 11:33:00 AM

0

Ben Bacarisse wrote:

> [â?¦] 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.

You are wrong.

> There might be some cases where this might be used in code,

There are.

> but it does not meet Stefan's other criterion as it is far too unwieldy
> to use in text.

Pardon?

> 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.

AISB, in strict mode this function returns the â??undefinedâ? value.

Strict mode is not a "corner case" anymore.

It is _not_ going to work this way.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Steven D'Aprano

5/29/2015 11:51:00 AM

0

On Fri, 29 May 2015 09:33 pm, Thomas 'PointedEars' Lahn wrote:

> Ben Bacarisse wrote:
>
>> [â?¦] 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.
>
> You are wrong.

Care to give an example of where the above does not return the global
object?


>> There might be some cases where this might be used in code,
>
> There are.
>
>> but it does not meet Stefan's other criterion as it is far too unwieldy
>> to use in text.
>
> Pardon?

Stefan wants to write text like:

"Math.cos can be used to calculate the cosine"


where Math cannot be shadowed or overridden by some other object. It is far
too unwieldy to say instead:

"(function () { return this; }).call(null).Math.cos can be
used to calculate the cosine."


Even if this is technically correct, it is far too unwieldy to use, and it
is misleading as well.


>> 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.
>
> AISB, in strict mode this function returns the â??undefinedâ? value.

It seems to work in Rhino:

[steve@ando ~]$ rhino -strict
Rhino 1.7 release 0.7.r2.3.el5_6 2011 05 04
js> (function () { return this; }).call(null)
[object global]


> Strict mode is not a "corner case" anymore.
>
> It is _not_ going to work this way.



--
Steven

Ben Bacarisse

5/29/2015 1:58:00 PM

0

Steven D'Aprano <steve@pearwood.info> writes:

> On Fri, 29 May 2015 09:33 pm, Thomas 'PointedEars' Lahn wrote:
>
>> Ben Bacarisse wrote:
>>
>>> [â?¦] 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.
<snip>
>>> 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.
>>
>> AISB, in strict mode this function returns the â??undefinedâ? value.

Thanks for pointing that out. I would offer, instead,

Function("{return this;}").call(null)

but it does not solve the original problem as the the identifier
'Function' is as unreliable as the original 'Math'. (The same is true
of my original suggestion in that the call property might have been
changed.)

> It seems to work in Rhino:
>
> [steve@ando ~]$ rhino -strict
> Rhino 1.7 release 0.7.r2.3.el5_6 2011 05 04
> js> (function () { return this; }).call(null)
> [object global]

Yes, but rhino is rather old and, I think, follows version 3 of the
language standard. The behaviour of a null 'this' changed between
versions and I think this accounts for difference in strict mode.

(Installing nodejs will give you a much more up-to-date ES environment,
and one with very may more library modules.)

<snip>
--
Ben.

Michael Haufe (\"TNO\")

5/29/2015 2:24:00 PM

0

On Thursday, May 28, 2015 at 4:02:23 PM UTC-5, Thomas 'PointedEars' Lahn wrote:
> Michael Haufe (TNO) wrote:
>
> > 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)
>
> This is not going to work. Strict mode is hereditary.

You are correct that it is hereditary, but I am correct (if not over-general) when I said: 'a scope without "use strict"'. I left out the work "lexical' on purpose.

I'll take it as valid criticism that I should have been more specific on this point