Using “var this” inside remote CFC methods
-
19-06-2021 - |
Domanda
I've inherited a project where there are a number of remote CFC's opened up for some Ajax requests and inside most methods in the CFC have the following:
<cfset var this.response = true />
Now I've never seen the var
and this
scope used together like this so I'm really not sure what to make of it so I guess my questions is:
Are there any issues with how this was coded? If so, are they major enough that I should put in
the effort to update all the CFC's to something like <cfset var
req.response = true />
?
Here is a quick example of what I'm seeing:
<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>
.
Updates:
- Based on the feedback/answers below I've replace all instances of
var this
. Thanks again to everyone that helped out!
.
Soluzione
Using var this
is the same as using this
.
Dumping the local
scope will include local variables as well as the Arguments
and This
scopes. (Can't find this documented; but I get this result in a bare cfc, and you got it in your screenshots.)
Because your function is access="remote"
you'll be getting a new instance of the cfc on every call, and therefore a bare This
scope. So those are "safe", but still a bad idea.
If there is any use of var this
in non-remote functions then you will be getting undesired persistence and may suffer race conditions that result is invalid data.
Relevant CF documentation:
Altri suggerimenti
update: upon checking your dump, the "this" in var this
is still this this
scope, not local.this.
It is setting the response to the this
scope, and it works in this case because because the CFC is instantiated every time it's being invoked remotely. However, it'd be best to rename this
into something else to ensure thread-safety in case the method is invoked by other CFC as public method.