What about ES6? You can use the arrow function .
This will:
myFunctions.forEach( f => f());
You can already use it today with a tool like babel . For an overview of ES6 features, run es6features .
EDIT:
You can expand the object Arrayas follows:
Array.prototype.invokeFunctions = function () {
this.forEach(function (f) {
if (typeof(f) === "function")
f()
});
}
var functions = [
function () { console.log(1);},
function () { console.log(2);}
];
functions.invokeFunctions();
1
2
, , Array. , . :
function FunctionList(functions) {
this.functions = functions;
}
FunctionList.prototype.callAll = function () {
this.functions.forEach(function (f) {
if (typeof(f) === "function")
f()
});
}
var functionList = new FunctionList(functions);
functionList.callAll();
, . , , FunctionList. , getter setter, / .