Skip to content

Instantly share code, notes, and snippets.

@bradmarshall
Created December 1, 2016 16:00
Show Gist options
  • Save bradmarshall/78159fdabe1e667a54f5ea8ced13f3c0 to your computer and use it in GitHub Desktop.
Save bradmarshall/78159fdabe1e667a54f5ea8ced13f3c0 to your computer and use it in GitHub Desktop.
Illustrates scoping of external variables in a Coldfusion Closure. If you create a variable in "local" scope outside of the closure function and wish to reference it from *inside*, you must leave the inside variable Unscoped in order to allow the engine to search through various scopes in order to find the external "local". The order the engine …
local.people = {
"jim": 100,
"bob": 500,
"sally": 1000
};
local.myStruct = {};
structEach(local.people, function(key, value) {
writeDump(key); // name
writeDump(value); // number
// Important: myStruct must remain UNSCOPED here.
// A reference to local.myStruct from inside this
// function will fail with a error stating that
// myStruct does not exist in local!
structAppend(myStruct, { #key#: #value# });
});
// Contains all of the same data from local.people
writeDump(local.myStruct);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment