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 {} ]