Getting All Variables In Scope

前端 未结 10 2345
小蘑菇
小蘑菇 2020-11-22 09:10

Is there a way to get all variables that are currently in scope in javascript?

10条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 09:43

    As everyone noticed: you can't. But you can create a obj and assign every var you declare to that obj. That way you can easily check out your vars:

    var v = {}; //put everything here
    
    var f = function(a, b){//do something
    }; v.f = f; //make's easy to debug
    var a = [1,2,3];
    v.a = a;
    var x = 'x';
    v.x = x;  //so on...
    
    console.log(v); //it's all there
    

提交回复
热议问题