Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

General Javascript ? about identifying objects.

LEGEND ,
Jun 01, 2019 Jun 01, 2019

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.

TOPICS
Scripting
573
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 02, 2019 Jun 02, 2019

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;

Translate
Adobe
Community Expert ,
Jun 02, 2019 Jun 02, 2019

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 02, 2019 Jun 02, 2019

I can't honestly say I understood a word of what you said, but I'll try to digest it. Thanks, pixxxel!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 02, 2019 Jun 02, 2019

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)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 05, 2019 Jun 05, 2019

Was that a better explanation?

Or do you still have questions about?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2019 Jun 05, 2019
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines