Can I store JavaScript functions in arrays?

前端 未结 9 1889
时光说笑
时光说笑 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 05:55

    You want an object literal, not an array.

    x = { 'dothis': function() { alert('hi'); } };
    

    Object

    x['dothis']()
    

    You can also dynamically invoke

    y = 'dothis';
    x[y]()
    

    Static/hard coded invocation:

    x.dothis()
    

    If you do want an array though:

    x = [function(){alert('hi');}][0]()
    

提交回复
热议问题