简体   繁体   中英

How to get all variables in the local scope of a process nodejs

How do you get all of the variables from the local scope ? For example :

x=1;

class testit {
  constructor (){
    this.a='two';
  }
}

var t1=new testit;

var missing = new testit;

var missing2 = 1;

var vars = Object.keys(global);
console.log(vars)

Will print x, but none of the other defined vars.

EDIT : we have settled on the following solution, any comments ?

To allow this type of concept to be used which allows the program to trace all of our class instances without allowing human error in global additions, we are now inheriting a base class :

"use strict";

global.childClasses = [];

class BaseClass {
  constructor(){
    global.childClasses.push(this);
  }

  static print(){
    console.log(global.childClasses);
  }
}

module.exports = {
  BaseClass
}

We can now list all instances by calling the base class's static method like in this testExample.js file listing :

"use strict";
let BaseClass = require('./baseClass').BaseClass;

class Test1 extends BaseClass {}
let test1 = new Test1;

class Test2 extends BaseClass {}
let test2 = new Test2;

BaseClass.print();

Which outputs :

$ node ./testExample.js 
[ Test1 {}, Test2 {} ]

How do you get all of the variables from the local scope ?

You don't. Javascript does not have any way to iterate the variables declared in the local scope. You can only iterate variables that are in the global scope (either implicitly or purposely).

The usual work-around is to make your variables be properties on a local object and then you can iterate the properties of that local object at any time with Object.keys() for for/in .

In your example x is not explicitly declared in any scope so (if you're not running in strict mode), then Javascript makes it a global. If you are running in strict mode, it is an error to assign to a variable that is not declared (because it's a generally bad thing to do).

The other declared variables are in the node module scope which is the local function scope (each node module is wrapped in a function) and that function is called by the loader so every module has its own private function scope.

Will print x, but none of the other defined vars.

Because only x is a global. The other declared variables are local to the module wrapper function that the loader automatically puts all node modules into. In node.js, you cannot place code in the actual top/global scope. Your code is inside a function wrapper that node creates for your module.

When you do not declare the variable with let , var or const then that variable is considered to be global in NodeJS environment but this gives error when we try to run our code. So, the better way to declare a variable or method as a global is by assigning then as a property of a global object. Something like this:

global.x=1;

class testit {
  constructor (){
    this.a='two';
  }
}

global.t1=new testit();

global.missing = new testit();

global.missing2 = 1;
console.log(Object.keys(global));

With that you will get all the keys of global printed with the variable names t1 , missing and so on. Also, for creating a instance of a class you need to specify the parenthesis () like new testit() instead of new testit .

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