简体   繁体   中英

List variables created in anonymous function

If I have some Javascript code like the following...

(function(){
    var a = 'valueA';
    var b = 'valueB';
    var c = 'valueC';

    //Create a loop that prints the name of all variables created above
    for(var x in ?????){
        console.log(x);
    }
})();

How do I print out a list of the variables that have been declared and assigned inside the anonymous function.

正如上面在评论中提到的那样,这是不可能的,如此链接Javascript Reflection中所述

This is very easy if the variables are in an array:

function printVariable() {
   var newArray = [valueA, valueB, valueC....];
   var i;

   for (i = 0; i < newArray.length; i++) {
      console.log(newArray(i));
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM