函数之外的Coldfusion局部范围? [英] Coldfusion local scope outside of a function?
问题描述
在函数之外定义的局部作用域到底是什么?
What exactly is the local scope defined outside of a function?
考虑以下代码:
<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>
通过添加 var
关键字来更改 madVar2 赋值,如下所示:
Changing the madVar2 assignment by adding the var
keyword, like this:
function madness() {
var madVar = "madness variable";
var madVar2 = "madness two variable";
将产生以下输出:
推荐答案
Local
作用域只定义在函数内部,不能在函数外部使用.
The Local
scope is only defined within functions and should not be used outside of it.
在函数之外定义的变量,默认在variables
范围内.
Variables defined outside the functions, default to the variables
scope.
//that way
myVar = 0;
//will be the same as
variables.myVar = 0;
当您引用 local.madVar2
变量时,该变量是在函数外部初始化的,您实际上是在引用 variables中的
local.madVar2
code> 范围,即变量 madVar2
存储在名为 local
的结构中,该结构存储在 variables
范围中.
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);
在函数内部定义变量后尝试转储 variables
范围:
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.
这篇关于函数之外的Coldfusion局部范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!