Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JScript Global Variables

Status
Not open for further replies.

Udir

Technical User
Aug 16, 2001
111
US
I read that you can declare varables as global by not including the var infront of it, but when I do:

logFileName = "C:\\scripts\\testlog.txt";
writeLogLine("Hello");
function writeLogLine(msg)
{
var ForReading = 1, ForWriting = 2, ForAppending = 8;
var FTarget, File, MyDate, MyTime;
var d = new Date();
MyDate = (d.getMonth() + 1) + "/";
MyDate += d.getDate() + "/";
MyDate += d.getYear();
MyTime = d.toTimeString();


FTarget = new ActiveXObject('Scripting.FileSystemObject');
File = FTarget.OpenTextFile(logFileName, ForAppending, true);
File.Write(MyDate + " " + MyTime + '\t' + msg + '\r\n');
File.Close();
}


It bombs out. Suggestions or comments on if global variables exist?
 
I tested the code. It works great as long as you have the testlog.txt file in c:\scripts folder in your machine.

henpat
 
Any variable that is initialized inside a function using the var keyword will have a local scope. If a variable is initialized inside a function without var, it will have a global scope. A local variable can have the same name as a global variable.
I beleive any variable initialized outside a function is global.

Glen
 
Hmmm.. for some reason when I ran it the first few times I got an object expected error.

So if I declear the variables first and then use them in a subequent function the function changes the value of the global as long as I don't have another variable with the same name decleared in the function?

 
I've also had problems with variables declared outside of my functions not being recognized. I've found that if I prefix them with "window." resolved many of my issues.

e.g.
window.logFileName = "c:/...
 
I've never had any problems at all using global variables, and I've never needed to qualify them with "window.". But I've also never used local and global variables with the same name.

I do use "var" when declaring global variables (and usually with local variables as well).

There is some issue with when and where variables are declared that can cause confusion. Here's the section from MSDN on variable scope. Pay attention to the part highlighted in red.
-----------------------------------------------------------
Microsoft JScript has two scopes: global and local. If you declare a variable outside of any function definition, it is a global variable, and its value is accessible and modifiable throughout your program. If you declare a variable inside of a function definition, that variable is local. It is created and destroyed every time the function is executed; it cannot be accessed by anything outside the function.

A local variable can have the same name as a global variable, but it is entirely distinct and separate. Consequently, changing the value of one variable has no effect on the other. Inside the function in which the local variable is declared, only the local version has meaning.

var aCentaur = "a horse with rider,"; // Global definition of aCentaur.

// JScript code, omitted for brevity.
function antiquities() // A local aCentaur variable is declared in this function.
{

// JScript code, omitted for brevity.
var aCentaur = "A centaur is probably a mounted Scythian warrior";

// JScript code, omitted for brevity.
aCentaur += ", misreported; that is, "; // Adds to the local variable.

// JScript code, omitted for brevity.
} // End of the function.

var nothinginparticular = antiquities();
aCentaur += " as seen from a distance by a naive innocent.";

/*
Within the function, the variable contains "A centaur is probably a mounted Scythian warrior,
misreported; that is, "; outside the function, the variable contains the rest of the sentence:
"a horse with rider, as seen from a distance by a naive innocent."
*/

[red]It's important to note that variables act as if they were declared at the beginning of whatever scope they exist in. Sometimes this results in unexpected behaviors. [/red]
var aNumber = 100;
var withAdditive = 0;

withAdditive += aNumber; // withAdditive is now 100.
tweak();
withAdditive += aNumber; // withAdditive is now 200.

function tweak() {
var newThing = 0; // Explicit declaration of the newThing variable.
// The next statement, if it were not commented out, would generate an error.
// newThing = aNumber;
// The next statement assigns the value 42 to the local aNumber, implicitly declaring it.
aNumber = 42;
if (false) {
var aNumber; // This statement is never executed.
aNumber = "Hello!"; // This statement is never executed.
} // End of the conditional.
} // End of the function definition.

The statement that is commented out attempts to assign the value of the local variable aNumber to the local variable newThing. It fails, despite the fact that a local aNumber variable is defined elsewhere in the function, and therefore exists throughout. The aNumber variable does not have any assigned value at the point where this statement occurs in the code, and is thus undefined.
-----------------------------------------------------------

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Ok, I understand your example. So would it be possible to change the value of aNumber iside the function and it be global to the rest of the program?

I ask because I have several values declared like

IX and IY that I need to access in a function, assign that value to another variable, and then change globally.
 
Code:
var iAmGlobal = 1;

function incrGlobal() {
   iAmGlobal++;
}

function showGlobal() {
   alert(iAmGlobal);
}

incrGlobal();
showGlobal(); // This will display [b]2[/b]


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top