ColdFusion:在 CFC 中省略 variables 关键字是否安全? [英] ColdFusion: Is it safe to leave out the variables keyword in a CFC?
问题描述
在 ColdFusion 组件 (CFC) 中,是否必须为变量范围的变量使用完全限定名称?
In a ColdFusion Component (CFC), is it necessary to use fully qualified names for variables-scoped variables?
如果我改变这个,我会不会惹上麻烦:
Am I going to get myself into trouble if I change this:
<cfcomponent>
<cfset variables.foo = "a private instance variable">
<cffunction name = "doSomething">
<cfset var bar = "a function local variable">
<cfreturn "I have #variables.foo# and #bar#.">
</cffunction>
</cfcomponent>
这个?
<cfcomponent>
<cfset foo = "a private instance variable">
<cffunction name = "doSomething">
<cfset var bar = "a function local variable">
<cfreturn "I have #foo# and #bar#.">
</cffunction>
</cfcomponent>
推荐答案
创建变量时指定变量"没有关系,因为foo默认会放在变量范围内;但访问变量时会很重要.
It won't matter to specify "variables" when you create the variable, because foo will be placed in the variables scope by default; but it will matter when you access the variable.
<cfcomponent>
<cfset foo = "a private instance variable">
<cffunction name="doSomething">
<cfargument name="foo" required="yes"/>
<cfset var bar = "a function local variable">
<cfreturn "I have #foo# and #bar#.">
</cffunction>
<cffunction name="doAnotherThing">
<cfargument name="foo" required="yes"/>
<cfset var bar = "a function local variable">
<cfreturn "I have #variables.foo# and #bar#.">
</cffunction>
</cfcomponent>
doSomething("args") 返回我有 args 和一个 函数局部变量"
doSomething("args") returns "I have args and a function local variable"
doAnotherThing("args") 返回我有一个变量的私有实例和一个函数局部变量."
doAnotherThing("args") returns "I have a private instance of a variable and a function local variable."
这篇关于ColdFusion:在 CFC 中省略 variables 关键字是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!