Both of @jantimon's code samples are fine ways to solve this problem, but I'd like to suggest a third approach. Instead of a self-calling function expression or a function that returns a function, consider an ordinary named function:
for( var i = 0; i <= tab_array.length - 3; ++i ) {
setupTab( i );
}
function setupTab( i ) {
var element = document.getElementById( 'mini_' + i );
element.addEventListener( 'click', function() {
open_tab( i );
});
}
This is essentially the same code as the self-calling function, but with these advantages:
- Familiarity. It's just an ordinary function call.
- Shorter and simpler loop body.
- With the self-calling function, the function parameter and the actual argument passed in are at opposite ends of the function (parameter at the beginning, argument at the end). A separate function avoids that.
These are just minor advantages, to be sure—any of these approaches will do the trick. But I do like the clarity of this method.