Charles Robertson
06/01/2023, 12:45 PMfunction foo(){
var local = {};
local = {
strReturn = {
criteria = {}
},
strCompetenceTemplate = ββ,
aCriteria = [],
iGlobalCnt = 0,
lSelected = ""
};
WriteDump(var=local.strReturn);
}
When I try this, I get an error saying that the local scope is empty.sknowlton
06/01/2023, 12:49 PMlocal is a built-in scope. var local would ... create local.local no? what are you trying to do here that you can't just do var someStruct = { myStuff }; and dump someStruct?Charles Robertson
06/01/2023, 1:38 PMfunction foo(){
local = {
strReturn = {
criteria = {}
},
strCompetenceTemplate = "",
aCriteria = [],
iGlobalCnt = 0,
lSelected = ""
};
WriteDump(var=local.strReturn);
}
This throws an error, saying that the local scope is empty.
Essentially, why do you have to do this:
function foo(){
local.strReturn = {
criteria = {}
};
local.strCompetenceTemplate = "";
local.aCriteria = "";
local.iGlobalCnt = 0;
local.lSelected = "";
WriteDump(var=local.strReturn);
}
This does not throw an errorsknowlton
06/01/2023, 1:38 PMvariables.local or local.localsknowlton
06/01/2023, 1:38 PMsknowlton
06/01/2023, 1:39 PMlocal.strReturn would be var strReturn = { stuff } inside the methodCharles Robertson
06/01/2023, 1:40 PMvar local = {};
Does not create:
local.local
var local = {};
Implicitly initialises the LOCAL scope. It is used for backward compatibility to ACF9sknowlton
06/01/2023, 1:40 PMCharles Robertson
06/01/2023, 1:40 PMfunction foo(){
var local = {};
local.strReturn = {
criteria = {}
};
local.strCompetenceTemplate = "";
local.aCriteria = "";
local.iGlobalCnt = 0;
local.lSelected = "";
WriteDump(var=local.strReturn);
}sknowlton
06/01/2023, 1:41 PMvar local = {} in that blockCharles Robertson
06/01/2023, 1:41 PMsknowlton
06/01/2023, 1:41 PMsknowlton
06/01/2023, 1:41 PMlocalCharles Robertson
06/01/2023, 1:42 PMlocal.local theorysknowlton
06/01/2023, 1:42 PMvar local = {} would create local.local but I haven't used ACF in forever. I'm not surprised it has special significance for backwards compatibility, a lot of ACF behavior doessknowlton
06/01/2023, 1:42 PMCharles Robertson
06/01/2023, 1:43 PMlocal, rather than explicitly reference localCharles Robertson
06/01/2023, 1:44 PMCharles Robertson
06/01/2023, 1:44 PMsknowlton
06/01/2023, 1:44 PMCharles Robertson
06/01/2023, 1:45 PMCharles Robertson
06/01/2023, 1:45 PMCharles Robertson
06/01/2023, 1:46 PMlocal scope variables in a single structsknowlton
06/01/2023, 1:46 PMCharles Robertson
06/01/2023, 1:48 PMCharles Robertson
06/01/2023, 1:48 PMsknowlton
06/01/2023, 1:51 PMfoo() in this example has a bunch of baked-in defaults, I think the reason you don't see this very often is one of:
1. Is foo() better off as a component with a constructor, given that it has significant state elements
2. Is foo() better off as a method with arguments and defaults, if any of the items being declared ever change;
3. Is foo() better off referencing static variables in the parent component, if indeed those things don't change, since they would appear to be statically defined values
4. If none of the above are true and all the contents of local are just 'things this method is operating on or returning' then I would just write it as var stuffRelevantToFoo = {} because the explicit references to local are way messier in terms of readability than just defining one struct in local which will still let you use your (very slightly shorter-hand) 'declare a bunch of variables' approachCharles Robertson
06/01/2023, 1:55 PMsknowlton
06/01/2023, 1:56 PMzackster
06/01/2023, 2:08 PMCharles Robertson
06/01/2023, 2:15 PMURL = {
foo: "bar",
bar: "foo"
}
OR
APPLICATION = {
foo: "bar",
bar: "foo"
}
I am not sure I have?
Maybe you cannot reassign or redeclare an in-built scope?
But you can add props to them, like const in JS
For instance in Javascript:
// In JS you can do this
const foo1 = {};
foo1.bar = "hello";
console.log("foo1: ", foo1);
// but you cannot do this
const foo2 = {};
foo2 = {
bar: "hello"
};
console.log("foo2: ", foo2);Charles Robertson
06/01/2023, 2:15 PMsknowlton
06/01/2023, 2:16 PMsknowlton
06/01/2023, 2:16 PMzackster
06/01/2023, 2:21 PMzackster
06/01/2023, 2:21 PMMatt Jones
06/01/2023, 2:33 PMfunction foo(){
structAppend(local, {
strReturn = {
criteria = {}
},
strCompetenceTemplate = "",
aCriteria = [],
iGlobalCnt = 0,
lSelected = ""
});
WriteDump(var=local.strReturn);
}sknowlton
06/01/2023, 2:34 PMsknowlton
06/01/2023, 2:35 PMCharles Robertson
06/01/2023, 2:36 PMMatt Jones
06/01/2023, 2:36 PMCharles Robertson
06/01/2023, 2:39 PMbdw429s
06/01/2023, 3:57 PMlocal. You can't override an entire scope anyway since scopes are more than a struct. It was one of those things were 99% of the code just had
var local = {};
so that was the one and only scenario that was allowed for. Basically, here is what happens
β’ var local = ANYTHING will simple be ignored.
β’ local = anything creates varaibles.local
It's been a hot minute since these decisions were made, but I think they were either in the Railo ticket tracker (now gone) or perhaps the Railo Google groupe (archived somewhere)Charles Robertson
06/02/2023, 8:54 AMCharles Robertson
06/02/2023, 9:05 AMvar local = {};
Does nothing useful.
But I wasnβt aware that the LOCAL scope was shorthand for:
variables.local
Are you saying that:
variables.local
Is an inbuilt scope?
I know:
variables
Is an inbuilt scope.
But how can:
variables.local
Be an inbuilt scope?
I must say I thought LOCAL was an inbuilt scope like URL, FORM, SESSION etc. Therefore it cannot be redeclared or reassigned, but it can be mutated by adding props.
Hence:
local = {
strReturn = {
criteria = {}
},
strCompetenceTemplate = "",
aCriteria = [],
iGlobalCnt = 0,
lSelected = ""
};
Does not work, because this is a redeclaration.
I am now wondering why ACF allowed such syntax?bdw429s
06/02/2023, 4:00 PMAre you saying that:
Copy codevariables.local
Is an inbuilt scope?What? Huh? No no, no, no.
variables.anythingHere is what you get when you run
anythingHere = ...
And if anythingHere happens to actually be the word local, then you get (most likely on accident) variables.local which is in no way shape or form any sort of "proper" scope. It's simply a key in the variables scope called anythingHere, or in this case local.bdw429s
06/02/2023, 4:00 PMI must say I thought LOCAL was an inbuilt scopeIt is.
bdw429s
06/02/2023, 4:02 PMI am now wondering why ACF allowed such syntax?ACF was always more lax with their scopes. I remember Micha talking about this years ago and how Railo was more protective, not allowing you to set anything into a base scope directly, but only into a key of said scope. The short answer to your question is likely, "Oversight, and lack of forethought/testing" but I assume it went back to the very early days of CF.
Charles Robertson
06/02/2023, 5:18 PMzackster
06/02/2023, 5:22 PMvar local ={}; and you could only have vars at the start of the function, ah the good old daysCharles Robertson
06/02/2023, 6:12 PMvar rule changed?
I remember when I was going to reprimand a Dev, for declaring a var half way through the function.
And when I tested the code, to my surprise, it worked π€©