0

In the buttons variable I create some buttons which references a function or an object.

In loop (l.15) this buttons is iterated, creating an action reference for each one.

My problem is: When the function referenced by action (l.18) is called, buttonAction at line 19, which should referer to variable created at line 16, is always the last one in the loop, in this case, buttons.Ok.

What am I doing wrong?

 1. var buttons = {
 2.     'Cancel': function () {
 3.         // stuff
 4.         },
 5.     'Ok': {
 6.     text: 'Ok',
 7.         id: 'btnOk',
 8.         click: function () {
 9.             // stuff
10.         }
11.     }
12. }
13. 
14. var buttonsMap = [];
15. for (var i in buttons) {
16.     var buttonAction = typeof buttons[i] == 'function' ? buttons[i] : buttons[i].click;
17.     var map = {
18.         action : function(arg) {
19.             buttonAction.apply(arg);
20.         }
21.     };
22.     buttonsMap.push(map);
23. }
24.
25. var instance = new Dialog({
26.     buttons : buttonsMap,
27. });
Moesio
  • 3,100
  • 1
  • 27
  • 36

0 Answers0