Reputation: 7853
What exactly is the local scope defined outside of a function?
Consider the following code:
<cfscript>
local.madVar2 = "Local scope variable";
function madness() {
var madVar = "madness variable";
madVar2 = "madness two variable";
writeOutput("local: <BR>");
writeDump(local);
writeOutput("========================================= <BR>");
writeOutput("local.madVar2: <BR>");
writeDump(local.madVar2);
writeOutput("<BR>========================================= <BR>");
writeOutput("madVar2: <BR>");
writeDump(madVar2);
writeOutput("<BR>========================================= <BR>");
writeOutput("variables.madVar2: <BR>");
writeDump(variables.madVar2);
writeOutput("<BR>========================================= <BR>");
}
</cfscript>
Changing the madVar2 assignment by adding the var
keyword, like this:
function madness() {
var madVar = "madness variable";
var madVar2 = "madness two variable";
Will yield this output:
Upvotes: 3
Views: 1505
Reputation: 1038
The Local
scope is only defined within functions and should not be used outside of it.
Variables defined outside the functions, default to the variables
scope.
//that way
myVar = 0;
//will be the same as
variables.myVar = 0;
When you refer to local.madVar2
variable, which was initialized outside the function you're essentially referring to the local.madVar2
in the variables
scope i.e the variable madVar2
is stored inside a struct named local
which is stored in the variables
scope.
So essentially, with the proper scoping in place your code is treated as:
writeOutput("variables.local.madVar2: <BR>");
writeDump(variables.local.madVar2);
Try dumping the variables
scope just after defining the variables inside the function as:
var madVar = "madness variable";
madVar2 = "madness two variable";
writeDump(variables);
.....
You will see how the variables fall into scopes.
Upvotes: 8