Access value of JavaScript variable by name?

前端 未结 7 1328
生来不讨喜
生来不讨喜 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:14

    var MyVariable = "Value of variable";    
    alert(readValue("MyVariable"));    
    
    // function readEValue(name) { readevalue -> readvalue // always do copy-paste to avoid errors  
    function readValue(name) {   
     return window[name]   
    }   
    

    That's all about ;o)

提交回复
热议问题