function in an array , how to declare and access?

programmerabhi profile image programmerabhi ・1 min read

var arr = [ function fun(){ console.log("this is a funtion ");} ];
console.log(arr0 );

"this is a funtion "

To get the output "this is a function", how to access array.
And do not appear undefined in output


markdown guide

var arr = [ function(){ return "this is a funtion"} ];
console.log(arr0 );

Output Console:
this is a funtion


You mean that only the anonymous function can be placed in the array.


You have more ways to handle functions in a Array. Any Function can include Into a Array. Array allowed all Types. Best way is to Use a pattern principel. Its a Use Case:

Want Array to handle manipulations of Data before Service called.
I Use Class Syntax for faster Sample:
Class Ainterceptor {
intercept(data){ // Do your Manipulation and Return Data }

Class Service{
constructor(interceptors: [new AInterceptor()])}
interceptors.forEach(i => data = i.intercept(data)