Coldfusion局部范围外的函数? [英] Coldfusion Local scope outside of a function?

查看:155
本文介绍了Coldfusion局部范围外的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在函数之外定义的局部作用域是什么?



考虑下面的代码:

 < cfscript> 

local.madVar2 =本地作用域变量;

function madness(){
var madVar =madness variable;
madVar2 =疯狂二变量;

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赋值,

  function madness(){
var madVar =madness variable;
var madVar2 =madness two variable;

将产生此输出:



img src =https://i.stack.imgur.com/WJnKY.pngalt =Image 2>

解决方案

Local 作用域仅在函数内定义,不应在其外部使用。



函数,默认为变量范围。

  // 
myVar = 0;
//将与
相同variables.myVar = 0;

当你引用 local.madVar2 ,它在函数之外初始化,实际上是指变量作用域中的 local.madVar2 madVar2 存储在名为 local 的结构中,该结构存储在变量 scope。



因此,在适当的范围设定下,您的代码将被视为:

  writeOutput(variables.local.madVar2:< BR>); 
writeDump(variables.local.madVar2);

在定义后尝试转储 variables 函数内的变量:

  var madVar =madness variable; 
madVar2 =疯狂二变量;
writeDump(variables);
.....

您将看到变量如何落入范围。 p>


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:

解决方案

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.

这篇关于Coldfusion局部范围外的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆