Can I store JavaScript functions in arrays?

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

    You can store things directly in an array, but as an object, for example:

    var Functions = { DoThis: function() { alert("do this"); } };
    
    Functions['DoThis'](); //alerts "do this"
    Functions.DoThis()     //alerts "do this"
    

    You can give it a try here.

提交回复
热议问题