# Why Does this code work, but this doesn't?

**URL:** <https://forums.creativeclouddeveloper.com/t/why-does-this-code-work-but-this-doesnt/5899>\
**Category:** UXP Plugin API\
**Tags:** bug, javascript, api-feedback\
**Created:** [February 22, 2023, 2:10am UTC](https://forums.creativeclouddeveloper.com/t/why-does-this-code-work-but-this-doesnt/5899 "2023-02-22T02:10:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zob](https://avatars.discourse-cdn.com/v4/letter/z/87869e/32.png) [@Zob](https://forums.creativeclouddeveloper.com/u/Zob)\
**Post date:** [February 22, 2023, 2:10am UTC](https://forums.creativeclouddeveloper.com/t/why-does-this-code-work-but-this-doesnt/5899/1 "2023-02-22T02:10:31Z")

</div>

It would seem that activeDocument.layers simply doesn’t function properly, and I can’t figure out why.

**Code That Works**

```auto
function WorkingNames() {
  const app = require("photoshop").app;
  const allLayers = app.activeDocument.layers;
  const allLayerNames = allLayers.map(layer => layer.name);
  const sortedNames = allLayerNames.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
  for (let bla in sortedNames){
    console.warn(sortedNames[bla])
  }
}

```

**Code That DOESNT Work**

```auto
function BrokenNames() {
  const app = require("photoshop").app;
  const allLayers = app.activeDocument.layers;
  //const allLayerNames = allLayers.map(layer => layer.name);
  //const sortedNames = allLayerNames.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
  for (let bla in allLayers){
    console.warn(bla.name)
  }
}

```

When you inspect the result of allLayers in the debugger for both forms of the function after that line executes, it is an empty array in both cases, yet somehow using the magic `allLayers.map(layer => layer.name) `  
causes allLayers to be populated, and we get the expected output.

What am I doing wrong? Is this a rookie mistake on my part, or a very large bug?

---

<div class="post-metadata">

**Author:** ![Maher](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/maher/32/1432_2.png) [@Maher](https://forums.creativeclouddeveloper.com/u/Maher)\
**Post date:** [February 22, 2023, 3:20am UTC](https://forums.creativeclouddeveloper.com/t/why-does-this-code-work-but-this-doesnt/5899/2 "2023-02-22T03:20:15Z")

</div>

`app.activeDocument.layers` is a proxy / not an actual array.  
`for-in` does not work with proxies ([source](https://ecmascript-daily.github.io/2016/02/10/why-remove-enumerate-and-reflect-enumerate))

use `for (let bla of allLayers){` the key is using `of` instead of `in`.

it seems like array high order functions (map and sort) return an actual (new) array that works with for-in

---

<div class="post-metadata">

**Author:** ![Zob](https://avatars.discourse-cdn.com/v4/letter/z/87869e/32.png) [@Zob](https://forums.creativeclouddeveloper.com/u/Zob)\
**Post date:** [February 22, 2023, 4:49pm UTC](https://forums.creativeclouddeveloper.com/t/why-does-this-code-work-but-this-doesnt/5899/3 "2023-02-22T16:49:29Z")

</div>

Wow, so much to learn about Javascript! I have never heard of proxies or traps before. They appear to be magic to the untrained eye. Its especially confusing that proxies don’t reveal their data while on a breakpoint and inspecting them after assignment, instead appearing as empty versions of whatever they are “wrapping”. I suppose that might be from some kind of custom trap for an essential function?

I’ll sum up my learning here and provide links to the articles that helped me understand proxy and trap.

A **proxy** creates an instance (or is it a pointer?) of an object with the primary intention of changing how it’s essential functions work (e.g. enumerate, which is used during `for in `, and getters and setters, etc.).

And a **trap** is the actual new function that replaces each essential function. Hilariously, they work well as real-life traps, too.

A **handler** is the object that contains the traps (which are just functions). When you define a new proxy, you pass in the handler as well, thus creating a trap-riddled fake object.

Simple and clear explanation on both Proxy and Trap

> **[JavaScript Proxy Explained Clearly By Practical Examples](https://www.javascripttutorial.net/javascript-proxy/)**
>
> A JavaScript Proxy is an object that wraps another object (target) and intercepts the fundamental operations of the target object.

Some more indepth info on them:

> **[Looking at All 13 JavaScript Proxy Traps | DigitalOcean](https://www.digitalocean.com/community/tutorials/js-proxy-traps)**
>
> A collection of code snippets to show all 13 Javascript Proxy traps in action.

---

<div class="post-metadata">

**Author:** ![Jarda](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/jarda/32/1909_2.png) [@Jarda](https://forums.creativeclouddeveloper.com/u/Jarda)\
**Post date:** [February 26, 2023, 8:45pm UTC](https://forums.creativeclouddeveloper.com/t/why-does-this-code-work-but-this-doesnt/5899/4 "2023-02-26T20:45:43Z")

</div>

I am experimenting with proxies and I think there might be a better way how to implement them in DOM.

`for in` could possibly work:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/1/16dcd98c725788e188d306fd00de67a07095694d.png)

And proxies could possibly show its content:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/b/b782177603ece88c075e16519641a218a982b462.png)

I will make a proposal (PS-104663) for change in DOM.

---

<div class="post-metadata">

**Author:** ![Maher](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/maher/32/1432_2.png) [@Maher](https://forums.creativeclouddeveloper.com/u/Maher)\
**Post date:** [February 27, 2023, 12:58pm UTC](https://forums.creativeclouddeveloper.com/t/why-does-this-code-work-but-this-doesnt/5899/5 "2023-02-27T12:58:45Z")

</div>

thanks @Jarda , that’d be great for everyone!

until then you can use:  
`Array.from(app.activeDocument.layers)`  
which return a normal array of layers

or the following function I created to get _ALL LAYERS_ regardless of “depth”  
it accepts one argument : the root of layers  
it could be doc’s layers or a group of layers

```auto
function flatLayers(lys) {
   return lys.flatMap((v) => v.layers ? [v].concat(flatLayers(v.layers)) : v)
}

```

and use it like so:  
`flatLayers(app.activeDocument.layers)`
