Does a function stop processing when another function is called from within it?
Have a look at the following function in a CFC (I'm using ColdFusion 9).
Assuming that oldObject is true
and that it is type 1
, does ColdFusion continue on until the end of the function and create the new object, or does it "bust out" on <cfset respond(result=false)>
and aborts any further processing in the function?
<cffunction name="myFunction" access="private">
<cfargument name="key">
<cfset oldObj = model("myModel").findOne(arguments.key)>
<cfset local.data = 1>
<cfif isObject(oldObj)>
<cfif oldObj IS 1>
<cfset respond(result=false)&开发者_JS百科gt;
<cfelse>
<cfset local.data = 2>
</cfif>
</cfif>
<cfset newObj.new(local.data)>
<cffunction>
The idea is this:
- If
oldObject
(1) exists and (2) is of type 1, bust out and don't do anything. - If oldObject exists and is NOT of type 1, then modify
local.data
and create new object. - If oldObject does not exist, just create the new object with unmodified
local.data
.
The respond() function simply returns data to the user via a JSON struct. I've omitted a lot of code since this is a theoretical question.
Your algorithm continues on to the end of the function. Only a <cfreturn>
would exit the function before the end. So <cfset newObj.new(local.data)>
will always be reached.
To output JSON inline (i.e. <cfoutput>#json#</cfoutput>
.), ensure respond()
does not specify <cffunction name="respond" output="false" ...>
.
Note, assuming the larger object these functions reside in is stateful, a cleaner approach would be having the client call methods as appropriate. Have the object internally cobble together the response as part of its state. And then finally, the client calling something along the lines of a getResponse()
function.
精彩评论