Google Apps Script/ menu functions with parameters

In Google Apps Script (GAS) there is limitation for menu functions, which cannot obtain parameters (aka. arguments). You define them as

Menu.addItem('Menu item display name', 'associatedFunctionName')

But there's no room for parameters. If you want to call function with parameters, you have to prepare a menuFuntion which calls function with parameter and create menu item which refers to that menuFunction.

Menu.addItem('Menu item display name', 'menuFunction')
...

function menuFunction() {
    return associatedFunctionName(param1, param2, ...)
}

But I had 10 menu items which called the same function, only with different parameter. In such case, you have to prepare 10 menuFunctions. Instead of writting them by hand, I went for dynamic function generation. It goes like this.

var menuItems = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var MENU_ITEM_FUNCTION_PREFIX = "menuFunction";
var MENU_ITEM_DISPLAY_PREFIX = "Alert ";

function onOpen(e) {
    function createMenu();
}

function createMenu() {
    // DocumentApp for Google Documents
    var menu = DocumentApp.getUi().createMenu("Test Menu");
    for (var i = 0, l = items.length; i < l; i++)
        menu.addItem(
          MENU_ITEM_DISPLAY_PREFIX + items[i],
          MENU_ITEM_FUNCTION_PREFIX + items[i]);            
    menu.addToUi();
}

/* this function will be called with different parameters */
function alertNum(num) {
    DocumentApp.getUi().alert(num)
}

/* generate all menuFunctions in immediatelly called closure */
(function (globalScope) {
    for (var i = 0, l = items.length; i < l; i++)
        createMenuFunction(items[i])

    // you need to generate menuFunction via another function,
    // because that way you generate scope to keep param unchanged.
    // If you'll generate menuFunction right inside for loop,
    // all your menuFunctions will refer to same instance of var i,
    // which will be undefined after this closure terminates.
    function createMenuFunction(param) {
        // here we define new function inside global scope
        globalScope[MENU_ITEM_FUNCTION_PREFIX + param] = function() {
           alertNum(param);
        }
    }
})(this) // here we passing global scope and call the closure

This way you can hack the GAS limit of non parameterized menu item functions.