在远程CFC方法中使用“var this”

时间:2012-07-09 21:35:18

标签: coldfusion coldfusion-9 cfc

我继承了一个项目,其中为一些Ajax请求打开了许多远程CFC,而内部 CFC中的大多数方法都有以下内容:

<cfset var this.response = true />

现在我从来没有见过这样一起使用的varthis范围,所以我真的不知道该怎么做,所以我想我的问题是:

这是如何编码的?如果是这样,他们是否足够重要,我应该投入 将所有CFC更新为 <cfset var req.response = true />

的努力

以下是我所看到的一个简单示例:

<cfcomponent>

    <cffunction name="check_foo" access="remote" returnformat="plain">

        <cfargument
          name     = "isfoo"
          type     = "string"
          required = "false"
          default  = "nope"
          hint     = "I check the string for foo"
          />

        <cfscript>

          /*setup new response*/
          var this.response = false;

          /*check for foo*/
          if( !findnocase( "foo", arguments.isfoo ) ) {

            /*no foo!*/
            this.response = false;

          }

          return this.response;

        </cfscript>

    </cffunction>

</cfcomponent>


更新

  1. 根据下面的反馈/答案,我将替换var this的所有实例。再次感谢所有帮助过的人!

2 个答案:

答案 0 :(得分:4)

更新:检查转储后,&#34;此&#34;在var this中仍然是this范围,而不是local.this。

它正在设置对this范围的响应,并且它在这种情况下有效,因为每次远程调用CFC都会被实例化。但是,最好将this重命名为其他内容以确保线程安全,以防该方法被其他CFC作为公共方法调用。

答案 1 :(得分:3)

使用var this与使用this相同。

转储local范围将包括局部变量以及ArgumentsThis范围。 (找不到这个记录;但是我在裸露的cfc中得到了这个结果,你在截图中得到了它。)

因为您的函数是access="remote",所以每次调用都会获得cfc的新实例,因此只有This范围。所以那些是“安全的”,但仍然是一个坏主意。

如果在非远程功能中使用var this,那么您将获得不受欢迎的持久性,并且可能会遇到导致无效数据的竞争条件。

相关CF文件:

Methods that are executed remotely through Flash Remoting and web services always create a new instance of the CFC before executing the method.

Variable values in the This scope last as long as the CFC instance exists and, therefore, can persist between calls to methods of a CFC instance.