JavaScript dynamic function name

前端 未结 4 1421
忘了有多久
忘了有多久 2021-01-27 15:19

I need to dynamically assign the name of a function to an element of an associative array. This is my attempt which does not work. The problem I am asking for help with is here

4条回答
  •  渐次进展
    2021-01-27 15:50

    I would use window[] and make sure its a function before trying to execute it since you don't have control over what is passed.

    var f = window[cr['cmd1']];
    if(typeof f==='function') {
      f(x);
    }
    

提交回复
热议问题