Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #27918
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Expression for the global object |
| Date | 2015-05-29 12:25 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87k2vrmxr4.fsf@bsb.me.uk> (permalink) |
| References | <JavaScript-20150528222318@ram.dialup.fu-berlin.de> <645d86ba-2299-4371-bb64-90dae6df316a@googlegroups.com> <cspehuFliqhU1@mid.uni-berlin.de> <5567f976$0$12987$c3e8da3$5496439d@news.astraweb.com> |
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.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Expression for the global object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2015-05-28 13:49 -0700
Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-28 22:59 +0200
Re: Expression for the global object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2015-05-29 07:24 -0700
Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-29 17:24 +0200
Re: Expression for the global object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2015-05-30 06:07 -0700
Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-30 15:20 +0200
Re: Expression for the global object Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-05-30 11:23 -0300
Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-30 19:05 +0200
Re: Expression for the global object Steven D'Aprano <steve@pearwood.info> - 2015-05-29 15:30 +1000
Re: Expression for the global object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-29 12:25 +0100
Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-29 13:33 +0200
Re: Expression for the global object Steven D'Aprano <steve@pearwood.info> - 2015-05-29 21:50 +1000
Re: Expression for the global object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-29 14:57 +0100
Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-31 16:18 +0200
Re: Expression for the global object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2015-05-29 07:31 -0700
Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-30 15:25 +0200
Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-30 18:56 +0200
csiph-web