Access value of JavaScript variable by name?

前端 未结 7 1319
生来不讨喜
生来不讨喜 2020-11-27 07:24

Hello it is possible to access the value of a JavaScript variable by name? Example:

var MyVariable = \"Value of variable\";


function readValue(name) {
             


        
7条回答
  •  情话喂你
    2020-11-27 08:21

    Try this ^_^

    var MyVariable = "Value of variable";
    
    alert(readValue("MyVariable"));
    
    function readValue(name) {
        return eval(name)
    }
    

提交回复
热议问题