Convert string to variable name in JavaScript

后端 未结 11 1555
无人及你
无人及你 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:29

    The window['variableName'] method ONLY works if the variable is defined in the global scope. The correct answer is "Refactor". If you can provide an "Object" context then a possible general solution exists, but there are some variables which no global function could resolve based on the scope of the variable.

    (function(){
        var findMe = 'no way';
    })();
    

提交回复
热议问题