Created
December 1, 2016 16:00
-
-
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 …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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