何时对 ColdFusion 组件中的变量进行 var 作用域? [英] When to var scope your variables in ColdFusion components?

查看:37
本文介绍了何时对 ColdFusion 组件中的变量进行 var 作用域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(a) 在什么情况下您应该对范围变量进行 var 以及 (b) 在什么情况下您不应该在 ColdFusion 组件中更改范围?

(a) What cases should you var scope variables and (b) what cases should you not var scope in a ColdFusion components?

推荐答案

当你在一个 CFC 中实现一个跨多个请求共享的函数时,你应该对你的变量进行 var 作用域(例如,Singleton,应用程序范围内的服务 CFC)

You should var scope your variables when you're implementing a function inside a CFC that is shared across multiple requests (i.e. Singleton, Service CFC's in Application scope)

如果每次都实例化 CFC,并且您的方法没有调用同一 CFC 中可能访问您在调用者方法.比如你直接通过 web 服务或者 ajax 调用的远程方法,它不会调用其他使用你没有 var scope 的 var 的方法,或者 CFWheels 中的 Controller.

You don't need to (yet still highly recommended to) var scope your variables if the CFC is instantiated every time, AND your method is not calling another method in the same CFC that may access the vars you've defined in the caller method. Such as a remote method that you called directly through web service or ajax, which doesn't call other methods that make use of vars you didn't var scope, or Controller in CFWheels.

您应该始终使用 var 关键字定义函数局部变量."每个 CFC 变量和范围文档 http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=buildingComponents_29.html

"You should always define function-local variables using the var keyword." per CFC variables and scope doc http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=buildingComponents_29.html

这篇关于何时对 ColdFusion 组件中的变量进行 var 作用域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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