Es gibt in JS mehrere Möglichkeiten Funktionen zu deklarieren. Hier einige davon.
function namedFunction(foo, bar) {
return foo + bar;
}
console.log('namedFunction:', namedFunction(4, 2));
// Console output: "namedFunction: 6"const noNamedFunction = function(foo, bar) {
return foo + bar;
}
console.log('noNamedFunction:', noNamedFunction(4, 2));
// Console output: "noNamedFunction: 6"const shortHandNoNameFunction = {
myArray: ['sna', 'fu'], // declare Array
get(index) { // get = freely definable function name
return this.myArray[index];
},
set(...elements) { // set = freely definable function name
this.myArray.push(...elements)
},
getMyArray(){
return this.myArray
}
}
console.log('shortHandNoNameFunction 1:', shortHandNoNameFunction.get(1));
// Console output: "shortHandNoNameFunction 1: fu"
shortHandNoNameFunction.set('42', 'qux')
console.log('shortHandNoNameFunction 2:', shortHandNoNameFunction.getMyArray());
// Console output: "shortHandNoNameFunction 2: (4) ['sna', 'fu', '42', 'qux']"let arrowShortFunction = (foo, bar) => foo + bar;
console.log('arrowShortFunction:', arrowShortFunction(4, 2));
// Console output: "arrowShortFunction: 6"// Immediately Invoke Function
(function(){
// doAnything
console.log('immediatelyInvokeFunction');
}
)();
// Console output: "immediatelyInvokeFunction"MoreComingSoon…
