Scoped variables are declared inside an <a-xxxx></a-xxxx>
element in a <let></let>
block, and initialized after the block.
The variable name must contain a dot .
or be an object if the name doesn't contain a dot.
Scoped variables in a.js allow developers to manage reactivity at different levels:
Global Scoped Variables
<a-xxxx>
elements.global
.<let>
global g.variable;
</let>
g.variable = "value"
Namespace Scoped Variables
namespace
attribute value.nsGlobal
.<let>
nsGlobal ns.variable;
</let>
ns.variable = ["value1",2,3]
Descendant Scoped Variables
dGlobal
. Usually in a <a-tagDef forTag="my-ancestor">
element<let>
dGlobal d.variable;
</let>
d.variable = { name : "value" }
Ancestor Scoped Variables
aGlobal
. Usually in a <a-tagDef forTag="my-descendant">
element<let>
aGlobal d.variable;
</let>
//here, in the scope, d.variable has the value { name : "value" } as long as the element in inside a `<my-ancestor><my-ancestor>`
Local Scoped Variables
<a-xxxx>
element.local
.<let>
local l.variable;
</let>
l.variable = 3
<a-closure>
<let>
global g.value;
nsGlobal ns.value;
dGlobal d.value;
aGlobal a.value;
local l.value;
</let>
watch(() => {
console.log('Global:', g.value);
console.log('Namespace:', ns.value);
console.log('Descendant:', d.value);
console.log('Ancestor:', a.value);
console.log('Local:', l.value);
});
</a-closure>
Initialize the variables after the closing </let>
of the declaration.
Any variable having the keyword watched
between the type (global
,...) and it's name.
<a-closure>
<let>
global watched global.value;
</let>
global.value = 42;
</a-closure>