
















Meta
<cffunction name="myFunction">
<cfset var mySafeVariable=1 />
<cfset myUnsafeVariable=1 />
</cffunction>
In the above snippet, two variables are created. The first variable uses the var keyword to ensure that the variable is local to the function, and if the same variable name existed elsewhere it won’t be overwritten. The second variable does not use var, and as such is not local, and variable conflicts can indeed occur. And so, when creating user defined functions or ColdFusion Component methods, the rule has always been to always prefix local variables with “var”.
<cfset local.myVar1=1>
<cfset var myVar1=1>
Both of these code snippets do the exact same thing. Variables with the var prefix are now automatically defined in the LOCAL scope. So a variable defined as:
<cfset var mySafeVariable=1>
can be safely accessed as:
<cfoutput>#LOCAL.mySafeVariable#</cfoutput> 此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。