How to access a static member from the constructor

class MockFamily implements IFamily { static instances: MockFamily[] = []; constructor (nodeClass: { new (): Node; }, engine: Engine) { MockFamily.instances.push(this); } /* sniiiiiip */ } 

In the above example, is there a way to access the instances static value from the constructor without using the actual class name?

+4
source share
1 answer

Static variables are always available through the class name. A class object acts like an object with properties. The closest you could come is perhaps:

 with (MockFamily) { instances.push(this); } 

Although I would not recommend it.

Modules are another thing. At run time, their contents are variable in the function area, and can be accessed almost anywhere inside.

 module MyModule { var instances: IFamily[] = []; export class MockFamily implements IFamily { constructor (nodeClass: { new (): Node; }, engine: Engine) { instances.push(this); } /* sniiiiiip */ } } 
+8
source

All Articles