Convert string to variable name in JavaScript

后端 未结 11 1558
无人及你
无人及你 2020-11-22 02:59

I’ve looked for solutions, but couldn’t find any that work.

I have a variable called onlyVideo.

\"onlyVideo\" the string gets passe

11条回答
  •  孤街浪徒
    2020-11-22 03:37

    var myString = "echoHello";
    
    window[myString] = function() {
        alert("Hello!");
    }
    
    echoHello();
    

    Say no to the evil eval. Example here: https://jsfiddle.net/Shaz/WmA8t/

提交回复
热议问题