How to use a lambda function with variables from outside?
Hello, I've got the following source code:
- function MyMenu()
- {
- menuEntries = [
- { name: 'First', action: function() { alert("First"); } } ,
- { name: 'Second', action: function() { alert("Second"); } } ,
- { name: 'Third', action: function() { alert("Third"); } } ,
- ]
- this.show = function()
- {
- $("#myMenu").append("<ul></ul>");
- for( i = 0; i < menuEntries.length; i++)
- {
- $("#myMenu ul").append('<li id="pm' + i + '">' + menuEntries[i].name + '</li>');
- $("#pm" + i).click(function() { menuEntries[i].action(); } );
- }
- }
- }
I wish to execute the lambda function given at menuEntries[i].action. The list is build, but at a click i've got the error that menuEntries[i] is not defined. What should I do? I think