Can I store JavaScript functions in arrays?

前端 未结 9 2036
时光说笑
时光说笑 2021-01-30 05:34

How can I store functions in an array with named properties, so I can call like

FunctionArray["DoThis"]

or even

Function         


        
9条回答
  •  悲&欢浪女
    2021-01-30 06:03

    You can actually do that. Just declare it outside the array, like so...

    const your_function = function(){ console.log("I am your function") }
    
    const group = [0, "lizard", false, your_function()]
    
    group[3]
    

    You may also change where it's called, if you want to...

    const your_function = function(){ console.log("I am your function") }
    
    const group = [0, "lizard", false, your_function]
    
    group[3]()
    

    Functions were named wrong :/ sry

提交回复
热议问题