473,372 Members | 1,033 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,372 software developers and data experts.

Set global variable from within a Class

From within a class instantiation, I'm trying to set the value of a
global variable. Let's call that variable "x". I know I can do this

window.x = 5;

and it will work but I really need to do this:

x = 5;

The above works in Firefox 2 but not in IE6. Actually, I'm trying to
do this

eval("x = 5");

For reasons that are too long to explain, I can't do this

eval("window.x = 5");

(which I realize would also work) because I can't alter the variable
name. It has to be "x"

Any thoughts on how I can do this within the scope of a Class? I know
this is non standard and might seem odd or inadvisable but it's a long
story that will be solved if someone can help with my problem.

Oct 2 '08 #1
2 3968
Never mind. The cause of my problem is something completely different.
See the thread "id names are global variables?"
Oct 2 '08 #2
On Thu, 02 Oct 2008 11:19:26 -0700, Bruce wrote:

Are you *sure* this is a good idea?
eval("x = 5");
Eval is very seldom the correct tool to use.
>
For reasons that are too long to explain, I can't do this

eval("window.x = 5");
If you *must* use eval, why can you not:
{ eval("x = 5"); window.x = x; }
Any thoughts on how I can do this within the scope of a Class? I know
this is non standard and might seem odd or inadvisable but it's a long
story that will be solved if someone can help with my problem.

Tested in Fx 3.0.2, IE6 and Opera (something)

<html>
<head>
<title>Test</title>
</head>
<body>

<script type="text/javascript">

function w(s)
{
document.write(s + "<br>");
}

var _global = (function() {return this})();
var x = 10;

(function() {
var x = 5;
w("Anon function start");
w("Inner: x= " + x);
w("_global.x= " + _global.x);

w("Showing we don't get a primitive reference");
x = _global.x; // This makes a COPY of _global.x
x = 8; // And this line doesn't affect global x at all
w("Inner: x= " + x);
w("_global.x= " + _global.x);
w("Anon function inside an anon function finds the 'closest' x");
(function() { // Even in a new scope, it will find "inner_x" first.
x = 18;
})();
w("Inner: x= " + x);
w("_global.x= " + _global.x);
w("As does eval");
eval("x = 22");
w("Inner: x= " + x);
w("_global.x= " + _global.x);
// Note: I CANNOT actually recommend that someone USE this method.
// The use of "with" is considered evil by many for very good reason.
// If this is the "solution" to a problem, I'm willing to wager you
have
// have far bigger problems.
//
// "with" is considered bad form by many. Don't do it.
w("But the WITH statement is ... magic");
with(_global) {
x = 15;
}
w("Inner: x= " + x);
w("_global.x= " + _global.x);
w("But the WITH statement is ... magic -- with eval");
with(_global) {
eval("x = 25");
}
w("Inner: x= " + x);
w("_global.x= " + _global.x);

})();
w("And the end, we have: global x= " + x);

</script>

</body>
</html>
Oct 2 '08 #3

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

Similar topics

8
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions>...
15
by: Relaxin | last post by:
How can I have a variable that has been initialized and set to a value within one source file (*.cs), and have access to that same variable in other files of the same project? NOTE: That...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
24
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think...
9
by: ruca | last post by:
How can I declare a global variable in my .js file, that I can preserve her value each time I need to call any function of .JS file in my ASP.NET application? Example: var aux=null; function...
8
by: Thomas Coleman | last post by:
Ok, I've obviously discovered that Global.aspx has been completely changed in ..NET 2.0. However, I haven't figured out how to declare a constant that's available to any page in my application...
3
by: Pierre | last post by:
Hello, In an aspx page (mypage.aspx) from a web projet, I would like to get the value of a variable of the projet that is declared as public in a module. The variable can be called from...
4
by: Sheldon | last post by:
Hi, I have a series of classes that are all within the same file. Each is called at different times by the main script. Now I have discovered that I need several variables returned to the main...
2
by: RgeeK | last post by:
I'm seeing something which make me think I'm missing something about how global var's behave. I've defined a global string, right at the start of my .py file. outXMLfile = "abc" I define a...
4
by: RgeeK | last post by:
I have a main module doStuff.py and another module utility.py. At the start of doStuff.py I call import utility.py Then I also proceed to initiallize some global variables sName = "" ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.