Re: Shell-Extension: unwanted behavior



On Thu, Aug 18, 2011 at 8:06 AM, Ole Ernst <oleernst web de> wrote:
> Hello everybody,
>
> I encountered a problem while writing a shell-extension. Based on a
> config-array (which gets loaded at start up) I want to create a variable
> number of PopupMenuItems, which should be connected to different functions.
>
> Here is the simplified code, which shows the unwanted behavior:
> http://pastebin.com/GsjzHVyK
>
> The problem is: no matter on what menu I click, the log always says
> Entry3 (last connected function). Actually I'd like it to show Entry1 to
> Entry3 based on the menu I click.
>
> Am I missing something? Is there another way of doing it? (Note: That's
> pretty much my first contact with javascript)

JavaScript has funky scoping rules.

First, make sure you use "const presets" or "let presets", otherwise
you're defining a global there.

Second, the problem is that you're declaring "item" *outside* of the
for loop, which means that there's one variable which is updated per
loop, and all the closures reference the same variable. Putting "let
item;" inside the loop body should make it scoped to the block itself.

> Thanks in advance!
>
> Ole Ernst
> _______________________________________________
> gnome-shell-list mailing list
> gnome-shell-list gnome org
> http://mail.gnome.org/mailman/listinfo/gnome-shell-list
>



-- 
  Jasper


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]