473,382 Members | 1,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Accessing the global object

Bob
Is there anyway to access the global object from inside a function other
than doing a "var _global = this;" before declaring the function?

Thanks
Feb 22 '08 #1
15 2018
"Bob" <no****@nowhere.comwrites:
Is there anyway to access the global object from inside a function other
than doing a "var _global = this;" before declaring the function?
If you know the name of the global and you're sure you're not overriding
the name, you can use the global name.

IWO, yes and no.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Feb 22 '08 #2
On Feb 21, 5:10 pm, "Bob" <nob...@nowhere.comwrote:
Is there anyway to access the global object from inside a function other
than doing a "var _global = this;" before declaring the function?
In a web browser the global object is usually available as a property
"window" of the global object. I think your idea of creating your own
"_global" is better. In ECMAScript 4 there will be a default "global"
property of the global object that references the global object. It
will work like "window" but "window" is a bad name when scripting in a
non-browser host.

Peter
Feb 22 '08 #3
Bob wrote:
Is there anyway to access the global object from inside a
function other than doing a "var _global = this;" before
declaring the function?
In javascript the value of the - this - keyword is determined by how a
function is called. If the function is not called as a method of an object
the value of the - this - keyword defaults to a reference to the global
object. As a result, from any context you can get a reference to the global
object using:-

function x(){
...
var localGlobalRef = function(){return this;}();
...
}

This is 100% reliable in ECMAScript 3rd Ed. implementations, and quite
useful, but it looks like ES 4 will not be back-compatible with ES 3 in this
regard. Still, not being back-compatible with ES 3 may (fingers crossed) be
enough to kill ES 4 in its cradle so maybe that is not worth worrying about.

Richard.
Feb 22 '08 #4
Bob
Interesting. Is there anyway to distinguish ES3 from ES4, and if ES4 is
present then return the global property?

Thanks very much
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in message
news:fp*******************@news.demon.co.uk...
Bob wrote:
>Is there anyway to access the global object from inside a
function other than doing a "var _global = this;" before
declaring the function?

In javascript the value of the - this - keyword is determined by how a
function is called. If the function is not called as a method of an object
the value of the - this - keyword defaults to a reference to the global
object. As a result, from any context you can get a reference to the
global
object using:-

function x(){
...
var localGlobalRef = function(){return this;}();
...
}

This is 100% reliable in ECMAScript 3rd Ed. implementations, and quite
useful, but it looks like ES 4 will not be back-compatible with ES 3 in
this
regard. Still, not being back-compatible with ES 3 may (fingers crossed)
be
enough to kill ES 4 in its cradle so maybe that is not worth worrying
about.

Richard.


Feb 22 '08 #5
AKS
On Feb 22, 2:54 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
but it looks like ES 4 will not be back-compatible with ES 3 in this
regard. Still, not being back-compatible with ES 3 may (fingers crossed) be
enough to kill ES 4 in its cradle so maybe that is not worth worrying about.
Will be this compatible with ES4?

var localGlobalRef = (function () { return this; }).call(null);

Feb 22 '08 #6
Peter Michaux wrote:
On Feb 21, 5:10 pm, "Bob" <nob...@nowhere.comwrote:
>Is there anyway to access the global object from inside a function
other than doing a "var _global = this;" before declaring the function?

In a web browser the global object is usually available as a property
"window" of the global object.
Nonsense. You are jumping to conclusions.
I think your idea of creating your own "_global" is better.
See <47**************@PointedEars.de>, among others.
In ECMAScript 4 there will be a default "global" property of the global
object that references the global object.
I am looking forward to that.
It will work like "window" but "window" is a bad name when scripting in a
non-browser host.
However, how is the issue of accessing properties of the Global Object of
another global execution context going to to be addressed, as with frames
and windows?
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Feb 24 '08 #7
On Feb 23, 5:37 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Peter Michaux wrote:
On Feb 21, 5:10 pm, "Bob" <nob...@nowhere.comwrote:
Is there anyway to access the global object from inside a function
other than doing a "var _global = this;" before declaring the function?
In a web browser the global object is usually available as a property
"window" of the global object.

Nonsense. You are jumping to conclusions.
The word "usually" usually indicates one is not making a definitive
conclusion.

I think your idea of creating your own "_global" is better.

See <47C0C97C.5010...@PointedEars.de>, among others.
In ECMAScript 4 there will be a default "global" property of the global
object that references the global object.

I am looking forward to that.
It will work like "window" but "window" is a bad name when scripting in a
non-browser host.

However, how is the issue of accessing properties of the Global Object of
another global execution context going to to be addressed, as with frames
and windows?
this.opener

this.parent

this.frames

Peter
Feb 24 '08 #8
On Feb 24, 3:20 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Peter Michaux wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Peter Michaux wrote:
On Feb 21, 5:10 pm, "Bob" <nob...@nowhere.comwrote:
Is there anyway to access the global object from inside a function
other than doing a "var _global = this;" before declaring the function?
In a web browser the global object is usually available as a property
"window" of the global object.
Nonsense. You are jumping to conclusions.
The word "usually" usually indicates one is not making a definitive
conclusion.

The word "usually" implies a perceived majority of cases where said
condition would apply.
Then I said what I meant. The overwhelming majority of JavaScript
hosts on earth are web browsers that have the global "window"
property.

>In ECMAScript 4 there will be a default "global" property of the global
object that references the global object.
I am looking forward to that.
>It will work like "window" but "window" is a bad name when scripting in a
non-browser host.
However, how is the issue of accessing properties of the Global Object of
another global execution context going to to be addressed, as with frames
and windows?
this.opener
this.parent
this.frames

That would imply ECMAScript Edition 4 is going to standardize properties of
Window host objects as built-in properties of the Global Object
I don't see a problem. As far as I know, they aren't standardizing the
properties of the global object, they are simply standardizing a way
to access the global object that can be sensibly used in a non-browser
host.

[snip <--- look Thomas, I snipped]

By the way, http://pointedears.de/scripts/ is still an error page.

Peter
Feb 24 '08 #9
On Feb 24, 10:36 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Peter Michaux wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Peter Michaux wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Peter Michaux wrote:
On Feb 21, 5:10 pm, "Bob" <nob...@nowhere.comwrote:
Is there anyway to access the global object from inside a function
other than doing a "var _global = this;" before declaring the function?
In a web browser the global object is usually available as a property
"window" of the global object.
Nonsense. You are jumping to conclusions.
The word "usually" usually indicates one is not making a definitive
conclusion.
The word "usually" implies a perceived majority of cases where said
condition would apply.
Then I said what I meant. The overwhelming majority of JavaScript
hosts on earth are web browsers that have the global "window"
property.

Then I'm afraid your argument is a fallacious one indeed.
You've said things like this many times. As far as I know you have not
ever listed where these other billions of user agents are that are
not web browsers similar to IE4+/NN4+. Who/What/Where/Why/When/How are
they?

Peter
Feb 24 '08 #10
On Feb 24, 2:04 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Peter Michaux wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Peter Michaux wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Peter Michaux wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Peter Michaux wrote:
>On Feb 21, 5:10 pm, "Bob" <nob...@nowhere.comwrote:
>>Is there anyway to access the global object from inside a function
>>other than doing a "var _global = this;" before declaring the function?
>In a web browser the global object is usually available as a property
>"window" of the global object.
Nonsense. You are jumping to conclusions.
The word "usually" usually indicates one is not making a definitive
conclusion.
The word "usually" implies a perceived majority of cases where said
condition would apply.
Then I said what I meant. The overwhelming majority of JavaScript
hosts on earth are web browsers that have the global "window"
property.
Then I'm afraid your argument is a fallacious one indeed.
You've said things like this many times. As far as I know you have not
ever listed where these other billions of user agents are that are
not web browsers similar to IE4+/NN4+. Who/What/Where/Why/When/How are
they?

Your continuously missing the point,
If you clearly state your point then I will not miss it.

I stated that the overwhelming majority JavaScript hosts are web
browsers similar to NN4+ and IE4+. By this I also mean browsers like
IE7, FF2, etc. I am right. You seem to think I am wrong but you are
not able to identify these billions of JavaScript hosts that I should
consider. I would like to know to which billions of JavaScript hosts
you refer. They don't exist so you cannot do so. Instead you are
resorting to nitpicking about who has the burden of proof. Where are
your billions of hosts, Thomas? I'll even take hundreds of millions of
hosts if you can come up with those.

If you are trying to make some other point completely then I'm
interested what that is. There is no need to be so oblique about it.
Just say what you want to say in plain language. It cannot be so
difficult.

Peter
Feb 24 '08 #11
Thomas 'PointedEars' Lahn said the following on 2/24/2008 5:04 PM:
Peter Michaux wrote:
>[...] Thomas 'PointedEars' Lahn [...] wrote:
>>Peter Michaux wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Peter Michaux wrote:
>[...] Thomas 'PointedEars' Lahn [...] wrote:
>>Peter Michaux wrote:
>>>On Feb 21, 5:10 pm, "Bob" <nob...@nowhere.comwrote:
>>>>Is there anyway to access the global object from inside a function
>>>>other than doing a "var _global = this;" before declaring the function?
>>>In a web browser the global object is usually available as a property
>>>"window" of the global object.
>>Nonsense. You are jumping to conclusions.
>The word "usually" usually indicates one is not making a definitive
>conclusion.
The word "usually" implies a perceived majority of cases where said
condition would apply.
Then I said what I meant. The overwhelming majority of JavaScript
hosts on earth are web browsers that have the global "window"
property.
Then I'm afraid your argument is a fallacious one indeed.
You've said things like this many times. As far as I know you have not
ever listed where these other billions of user agents are that are
not web browsers similar to IE4+/NN4+. Who/What/Where/Why/When/How are
they?

Your continuously missing the point, and making just more fallacious
arguments (here again: shifting the burden of proof), is becoming tiresome.

http://en.wikipedia.org/wiki/List_of_fallacies
You say they exist, someone else says they don't and asks you to prove
your argument. You don't and constantly whine about "shifting the burden
of proof". It is simple to prove what you say, just name a browser
without the window object being the global object. But no, you choose to
constantly whine about it and wonder why your credibility is at the
level it is.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 25 '08 #12
On Mar 2, 10:34 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:

[snip]
JFTR:http://wurfl.sourceforge.net/

So we have a maybe complete list of browsers used on mobile devices today.
However, that does not mean anything for tomorrow or user agents on other
devices.

So instead of having to check that list every day and update one's code
accordingly (which nobody could/would pay one for), one feature-tests for a
limited set of (in total) widely supported object models, including the
standardized one, and one will seldom have to change anything.
But how to choose that limited set? I have never read anyone's
concrete rules for making this choice but I have read criticism of the
mainstream library writers for their choices.
If it still happens that the code breaks in a user agent and that becomes
known to the developer, then one should maybe add a branch for that user
agent. That decision depends of course mostly on the bug in the user agent
that caused it, if it could be worked around at all. If not, the UA is
broken and should not be supported.
"maybe"? "mostly"?

I agree this is all grey area but people are frequently derided for
their choices in the grey areas. When pressed for justification I
haven't seen the critics present anything concrete.

Is it ok for a developer to say "I know this script destined for the
general web is broken in Internet Explorer 5.5 and Opera 9 but I don't
care?" It seems as though he would be ripped to shreds and accused an
idiot on c.l.js but he would simply respond "Those browsers are not
supported". That doesn't seem sufficient to the critics that don't
have objective support for their own similar decisions.

Peter
Mar 3 '08 #13
Thomas 'PointedEars' Lahn wrote:
Peter Michaux wrote:
>[...] Thomas 'PointedEars' Lahn [...] wrote:
>>So instead of having to check that list every day and update one's code
accordingly (which nobody could/would pay one for), one feature-tests for a
limited set of (in total) widely supported object models, including the
standardized one, and one will seldom have to change anything.
But how to choose that limited set? I have never read anyone's
concrete rules for making this choice but I have read criticism of the
mainstream library writers for their choices.

I think that if you support the W3C DOM, the MSHTML DOM, the Opera DOM,
Apple WebCore, the KHTML DOM, and the NS4 DOM, you can be pretty sure that
your application does not break on the mobile device. [...]
^^^^^^^^^^^^^^
That should have been "that you application works". Graceful degradation
and feature tests should prevent the application from breaking even if
exposed to an unknown DOM.
PointedEars
Mar 3 '08 #14
VK
<snip>
All I want from you is the definition of the "feature detection". You
either can give it or you have no idea what are you talking about. I
am not answering your questions until I see that.
Mar 14 '08 #15
On Mar 14, 10:30 am, VK <schools_r...@yahoo.comwrote:
<snip>
All I want from you is the definition of the "feature detection". You
either can give it or you have no idea what are you talking about. I
am not answering your questions until I see that.
Did Richard not write the following?

<URL: http://www.jibbering.com/faq/faq_notes/not_browser_detect.html>

That should pretty much explain it for you.

Peter
Mar 14 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Fernando Rodríguez | last post by:
Hi, I haven't used Python in quite some time, and I'm bit puzzled by this: counter = 0 class Blah(object): def run(self): counter += 1
2
by: C Gillespie | last post by:
Dear All, I have 2 arrays var A1 = new Array(); A1 ="Y2"; var B1 = new Array(); B1 ="Y1"; B1 ="sink";
2
by: Ekul | last post by:
I have an application that allows users to login and logout. I track how many users are logged in and when each individual is logged in. The application will not allow concurrent logins(let a...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
10
by: Christian Christmann | last post by:
Hi, I'm wondering if my small example is not "dangerous": #define SIZE 10 char global; char* globalPtr = global; int main()
12
by: Steve Blinkhorn | last post by:
Does anyone know of a way of accessing and modifying variables declared static within a function from outside that function? Please no homilies on why it's bad practice: the context is very...
2
by: rgparkins | last post by:
Hi Guys Maybe a simple and easy solution to this, but I keep getting exception object not set to instance etc etc. I have a Timer that is initiated in Application_Start in Global.asax. This...
10
by: Nemisis | last post by:
Hi everyone, I am trying to create a custom error page, that the user gets shown when a error has occurred within my website. The code works perfectly, apart from when an invalid URL is typed...
3
by: anoop.kn | last post by:
Is there anyway to access Global Function Pointer from its name ? I want to call the function at runtime during a script execution. PS: eval() works, I looking for a more efficient way of doing the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.