The local ColdFusion region inside the object is literal

I see some strange behavior when using the CF region in an object literal in function arguments. But only when executed inside a loop ...

Code example:

<cffunction name="f"> <cfoutput> <cfset LOCAL.foo = 123 /> <!--- Works fine ---> #serializeJSON({blah = LOCAL.foo})# <!--- Works fine ---> <cfloop from=1 to=1 index="i"> <cfset bar = {blah = LOCAL.foo} /> #serializeJSON(bar)# </cfloop> <!--- Element FOO is undefined in LOCAL ---> <cfloop from=1 to=1 index="i"> #serializeJSON({blah = LOCAL.foo})# </cfloop> </cfoutput> </cffunction> <cfset f() /> 

PS: serializeJSON() is, for example, goals. This happens in any function I tested, where one of the arguments is structure.

+4
source share
2 answers

Works well at Railo.

It also doesn't make any difference if you use any other container instead of the local scope, it is also impossible to catch it with cftry .

If you serialize only the local region in the loop:

 <cfloop from=1 to=1 index="i"> #serializeJSON(local)# </cfloop> 

Result:

 {"ARGUMENTS":{},"___IMPLICITARRYSTRUCTVAR1":{"BLAH":123},"___IMPLICITARRYSTRUCTVAR0":{"BLAH":123},"FOO":123} 

Looks like a mistake. Mind registration ?

+2
source

LOCAL is an area used only within functions. If you try to create the scope LOCAL variable outside the function, it will fail.

I will write a test and prove it to you in a minute ....

UPDATE Actually, I have CF 8 at work and cannot check it.

In CF8 and below, you can set LOCAL.Foo , but this is not really a CF area.

In CF9 and above, LOCAL can only be set internally.

 <cffunction> <cfset LOCAL.foo = 1> <cfreturn LOCAL.foo> </cffunction> 
+1
source

All Articles