gpt4 book ai didi

ColdFusion:在 CFC 中省略 variables 关键字是否安全?

转载 作者:行者123 更新时间:2023-12-04 16:19:00 24 4
gpt4 key购买 nike

在 ColdFusion 组件 (CFC) 中,是否有必要为变量范围的变量使用完全限定名称?

如果我改变这个,我会不会惹上麻烦:

<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 默认会放在变量范围内;但是当您访问变量时会很重要。

<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 函数局部变量

doAnotherThing("args") 返回“我有 变量的私有(private)实例 函数局部变量 。”

关于ColdFusion:在 CFC 中省略 variables 关键字是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com