Skip to content

Instantly share code, notes, and snippets.

@roryl
Last active March 11, 2016 10:08
Show Gist options
  • Save roryl/d14806856b35baeffc5e to your computer and use it in GitHub Desktop.
Save roryl/d14806856b35baeffc5e to your computer and use it in GitHub Desktop.
A basic empty closure
<cfscript>
myArray = ["one","two","three"];
newArray = myArray.map(function(value){
return ucase(value);
});
dump(newArray); //outputs ONE, TWO, TREE
</cfscript>
<cfscript>
myclosure = function(){
}
</cfscript>
<cfscript>
outerClosure = function(){
var outer = "foo";
innerClosure = function(){
var inner = "bar";
return outer;
}
echo(innerClosure()); //Outputs foo, the innerClosure had access to the outer variable
return inner; //Errors for inner being undefined, the outer did not have access to the inner
}
otherClosure = function(){
echo(outer); //Errors for being undefined
echo(inner); //Errors for being undefined
}
//outerClosure(); //Call outer closure
//otherClosure(); //Call the other closure showing that none of the interal variables can be accessed
</cfscript>
/**
* My xUnit Test
*/
component extends="testbox.system.BaseSpec"{
/*********************************** LIFE CYCLE Methods ***********************************/
// executes before all test cases
function beforeTests(){
}
// executes after all test cases
function afterTests(){
}
// executes before every test case
function setup( currentMethod ){
}
// executes after every test case
function teardown( currentMethod ){
}
/*********************************** TEST CASES BELOW ***********************************/
// Remember that test cases MUST start or end with the keyword 'test'
function testBasicClosure() test{
include template="/examples/closures/basic_closure.cfm";
expect(isClosure(myClosure)).toBeTrue();
}
function testAnonymousClosure() test{
include template="/examples/closures/anonymous_closure.cfm";
expect(newArray).toBe(["ONE","TWO","THREE"]);
}
function testClosureScopes() test{
include template="/examples/closures/closure_scopes_example.cfm";
expect(function(){outerClosure()}).toThrow();
expect(function(){otherClosure()}).toThrow();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment