Copy link to clipboard
Copied
I've been using "aDoc.layers.getByName("Name")" to target various elements. Recently I notices many use "aDoc.layers["Name"]" and "aDoc.layers.Name" to target elements. I guess the square brackets is pulling from an array of names for the given object category. Why does "no brackets" work? I have noticed it only works when there are no spaces in the name. Is that correct? What are the pros and cons of each approach. Is there a "best practice"? I like the "no bracket" approach if I learned to think ahead and build templates with no space-names.
A more difficult stuff to understanding.
This is not a 'simple' Array notation - this is an associative array object as:
object = { 'string': 'value1' } is the same as
object ['string'] = 'value';
in array notation:
allLayers[theLayer.name] = theLayer;
allLayers["Layer5"] = theLayer;
another notation could be:
allLayers.Layer5 = theLayer;
Copy link to clipboard
Copied
A more difficult stuff to understanding.
This is not a 'simple' Array notation - this is an associative array object as:
object = { 'string': 'value1' } is the same as
object ['string'] = 'value';
in array notation:
allLayers[theLayer.name] = theLayer;
allLayers["Layer5"] = theLayer;
another notation could be:
allLayers.Layer5 = theLayer;
Copy link to clipboard
Copied
I can't honestly say I understood a word of what you said, but I'll try to digest it. Thanks, pixxxel!
Copy link to clipboard
Copied
It is as I mentioned before not an easy stuff. And it is not easy for me to describe it in a language which is not my mother language.
I try it anyway.
But maybe helpful: looking for the differences between an array vs an object.
Your example
app.active.document.layers
looks like an array. But it is an object. That why the layers name could work as an object "key" with his own properties.
see the following examples:
// "simple" array
var arr = ["one", "two", "three"]
alert(arr[0]); // one
alert(arr["one"]); // undefined
// object or "array object"
var theLayers = app.activeDocument.layers; // be sure one layer name is Layer1 !!! without a space inbetween/before/behind the name
alert(theLayers); // [Layers]
alert(theLayers["Layer1"].name); // Layer1
alert(theLayers.Layer1.name); // Layer1
alert(theLayers.Layer1.visible); // true (or false)
alert(theLayers.Layer1.locked); // false (or true)
Copy link to clipboard
Copied
Was that a better explanation?
Or do you still have questions about?
Copy link to clipboard
Copied
Yes, thank you! I was not referring to your command of English in the slightest! You do great. I am alluding to my own lack of programming knowledge. I appreciate all the help you, and others provide.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now