bdw429s
12/07/2023, 8:38 PMbdw429s
12/07/2023, 8:39 PMvariables.inVariables = "bar"
url.inURL = "bar"
// updates variables scope, as expected
inVariables = "brad"
// Does NOT find url var and instead creates a new one in the variables scope
inURL = "brad"
writedump(variables)
writedump(url)bdw429s
12/07/2023, 8:40 PMbdw429s
12/07/2023, 8:41 PMfunction func() {
local.inLocal = "bar"
url.inURL = "bar"
// updates local scope, as expected
inLocal = "brad"
// Does NOT find url var and instead creates a new one in the variables scope
inURL = "brad"
writedump(local)
writedump(url)
}
func()Ryan Albrecht
12/07/2023, 8:42 PMbdw429s
12/07/2023, 8:42 PMRyan Albrecht
12/07/2023, 8:43 PMbdw429s
12/07/2023, 8:43 PMinLocal = "brad"
above "finds" the existing local.inLocal var and updates it insteadbdw429s
12/07/2023, 8:43 PMDave Merrill
12/07/2023, 8:43 PMbdw429s
12/07/2023, 8:44 PMbdw429s
12/07/2023, 8:44 PMRyan Albrecht
12/07/2023, 8:44 PMDave Merrill
12/07/2023, 8:48 PMbdw429s
12/07/2023, 8:49 PMbdw429s
12/07/2023, 8:50 PMfunction foo( param ) {
// updates arguments.param, does NOT create a new variables.param
param = "something else"
dump(arguments)
}
foo( "initial value" )bdw429s
12/07/2023, 8:51 PMfoo like
#foo#
you'd be accessing the same foo that you'd be setting if you wrote
foo = "bar"
but it only appears to work in some cases/scopesbdw429s
12/07/2023, 8:56 PMurl.foo = "bar";
baz = foo
foo = "new value"
It would seem reasonable, that the foo being used on the right hand side of the assignment is the same as the foo being set on the left hand side, but they are different variables/scopes.Dave Merrill
12/07/2023, 8:59 PM<cfscript>
public function test(string foo="", string bar="")
{
writeOutput("foo=#foo# arguments.foo=#arguments.foo#<br>");
foo = 1;
writeOutput("foo=#foo# arguments.foo=#arguments.foo#<br>");
writeOutput("bar=#bar# arguments.bar=#arguments.bar#<br>");
arguments.bar = 1;
writeOutput("bar=#bar# arguments.bar=#arguments.bar#<br>");
}
test();
</cfscript>bdw429s
12/07/2023, 9:01 PMmethod's variables scope.To be clear, a method doesn't have a variables scope. I can simply see the CFC or template's that it's running inside of.
bdw429s
12/07/2023, 9:03 PMlocal.foo and an arguments.foo inside the method.bkbk
12/08/2023, 12:48 PMinURL = "brad" within a function, then that is equivalent to variables.inURL = "brad".
The reason why the update local.inLocal = "bar"; inLocal = "brad"; happens without any confusion is because the second statement relies on the fact that inLocal is already stored in function-local scope.
You said,
It would seem reasonable, that the foo being used on the right hand side of the assignment is the same as the foo being set on the left hand side, but they are different variables/scopes.
I do think that they are indeed different. I do believe that the scope is determined by the left hand side of the assignment. Whereas the right hand side is the value of the assignment.
Hence, given
url.foo = "bar";
baz = foo;
(1) baz is in variables scope; (2) CFML determines its value by searching among the scopes for the existence of foo. It is found in the URL scope. So this code snippet tells us that the value of variables.baz is "bar".gavinbaumanis
12/10/2023, 5:41 AMgavinbaumanis
12/10/2023, 5:43 AM