July 12, 2018
Closures in ColdFusion Tags
Comments
(0)
July 12, 2018
Closures in ColdFusion Tags
Newbie 6 posts
Followers: 4 people
(0)

A Closure is an inner function that has access to outer function’s variables. Closures were earlier supported only in cfscript , now we have extended the functionality to support Closures in ColdFusion Tags as well. So now one can easily invoke Closures in ColdFusion Tags.

Some of the code-snippets for using this feature are as follows:

<cfset myarray=[
{name="Thomas", age="22"},
{name="Zaza", age="36"},
{name="Novak", age="136"},
{name="Marin", age="361"},
{name="Rafa", age="3"},
{name="$bl0091@", age="-23"}
]>
<!--- define closure function --->
<cfset closure=function (e1,e2){ return compare(e1.name,e2.name);
}>
<cfset ar = arraySort(myarray,closure)>
<cfdump var="#myarray#">
<cfscript>
// Define an array of structs myArray = [
{name="Thomas", age="22"},
{name="Zaza", age="36"},
{name="Novak", age="136"},
{name="Marin", age="361"},
{name="Rafa", age="03"},
{name="$bl0091@", age="-23"}
];

// Define a closure function that sorts the names in the array of structs callback=function (e1, e2){
return compare(e1.name, e2.name);
}

// Use the closure function 
arraySort(myArray,callback);

// Display the sorted array of structs 
WriteDump(myArray);
</cfscript>

Please try it out and let us know the feedback.

0 Comments
Add Comment