Skip to content

Instantly share code, notes, and snippets.

@abramadams
Last active January 1, 2016 23:28
Show Gist options
  • Select an option

  • Save abramadams/8216384 to your computer and use it in GitHub Desktop.

Select an option

Save abramadams/8216384 to your computer and use it in GitHub Desktop.
Here's a demonstration of how ColdFusion doesn't actually pass things by reference. (derived from http://cfmlblog.adamcameron.me/2012/08/complex-data-types-in-cf-and-how-theyre.html)
<cfscript>
st1 = {
one = "Tahi",
two = "Rua",
three = "Toru",
four = "Wha"
};
st2 = f(st1);
function f(st){
st = {
one = "Ichi",
two = "Ni",
three = "San",
four = "Shi"
};
return st;
};
writeOutput('<p>If struct was passed to the function by reference this would have changed both st1 and st2, but since it was by value-of-the-reference, when we re-built the st struct it overwrote the reference.</p>');
writeDump(var = st1, label = 'st1');
writeDump(var = st2, label = 'st2');
st3 = f2(st1);
function f2(st){
st.one = "changed";
return st;
};
writeOutput('<p>Here we see that just changing a key in st changed for both st1 and st3 ( since we didn''t overwrite the reference); </p>');
writeDump(var = st1, label = 'st1');
writeDump(var = st2, label = 'st2');
writeDump(var = st3, label = 'st3');
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment