quote:
Originally posted by davidt
Yes, functions are global and so can be called from anywhere. It's only the variables withinn functions that stay within the function.
functions can be perfectly local to another function too.
Just as variables can be global to all functions too.
code:
var Globe1 = "Hello World"
function DoTheBanana() {
var Localfruit = "Hello bananatown";
Debug.Trace('Localfruit: ' + Localfruit);
}
function DoTheConga() {
// Localfruit isn't valid in this function since it is declared locally inside DoTheBanana()
// It's scope begins and ends in the function DoTheBanana()
// Globe1 is valid in this function since it is declared globally.
Debug.Trace('Globe1: ' + Globe1)
var Localvar = "Hello bananatown";
// Notice that we can declare a local variable to this function and that it will keep its value, even if have another local variable with the same name inside the DoTheBanana() function.
var Localfruit = "I hate bananas";
DoTheBanana();
Debug.Trace('Localfruit: ' + Localfruit);
}
function NowForSomethingDifferent() {
// Let's create a local function which is only valid inside NowForSomethingDifferent()
function WoowLocalFunction() {
Debug.Trace('This is a local function')
}
// And call it
WoowLocalFunction();
}
Tip: try to not use global variables, unless it is absolutely nessecairly (goes for all languages you program in).
-------------------------------------------------------------------------
quote:
Originally posted by davidt
Does anybody know if Jscript is pass-by-value of pass-by-reference? I think it is the latter.
Numbers and Boolean values (true and false) are copied, passed, and compared by value. When you copy or pass by value, you allocate a space in computer memory and copy the value of the original into it. If you then change the original, the copy is not affected (and vice versa), because the two are separate entities.
Objects, arrays, and functions are copied, passed, and compared by reference.** When you copy or pass by reference, you essentially create a pointer to the original item, and use the pointer as if it were a copy. If you then change the original, you change both the original and the copy (and vice versa). There is really only one entity; the "copy" is not actually a copy, it's just another reference to the data.
When comparing by reference, the two variables must refer to exactly the same entity for the comparison to succeed. For example, two distinct Array objects will always compare as unequal, even if they contain the same elements. One of the variables must be a reference to the other one for the comparison to succeed. To check if two Arrays hold the same elements, compare the results of the toString() method.
strings are copied and passed by reference, but are compared by value. Note that if you have two String objects (created with new String("something")), they are compared by reference, but if one or both of the values is a string value, they are compared by value.
Note: When you pass a parameter to a function by value, you are making a separate copy of that parameter, a copy that exists only inside the function. Even though objects and arrays are passed by reference, if you directly overwrite them with a new value in the function, the new value is not reflected outside the function! Only changes to properties of objects, or elements of arrays, are visible outside the function.
**(To be very stricly correct that isn't entirly true though (JScript internally still makes a copy; but it also internally copies back the changed copy; aka: it isn't 'by reference' as in other languages <= hard to explain, though you shouldn't be bothered with it either, it's just per-info))