var myarr = test(); for (var j = 0; j < 10; j++) { myarr[j](); } function test() { var arr = []; for (var i = 0; i < 10; i++){ (function (x) { arr[x] = function () { console.log(x); } }(i)) } return arr; } var myarr = test(); for (var j = 0; j < 10; j++) { myarr[j](); }