<cffunction name="myFunction">
  <cfset var mySafeVariable=1 />
  <cfset myUnsafeVariable=1 />

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:

  • 文章标题: 《ColdFusion - Scope in cffunction》
  • 发布日期: 2019-01-01
  • 文章分类: Tech
  • 相关标签: ColdFusion