TypeScript: Global static variable best practice -
i have class need increment number each time class instantiated. have found 2 ways both ways works, not sure yet on best practice
declare variable in module scope
module m { var count : number = 0; export class c { constructor() { count++; } } }
declare variable in class scope , access on class
module m { export class c { static count : number = 0; constructor() { c.count++; } } }
my take example 2 not adds count variable in module scope.
see also: c# incrementing static variables upon instantiation
definitely method 2 since class using variable. should contain it.
in case 1 using variable become confusing once have more 1 classes in there e.g:
module m { var count : number = 0; export class c { constructor() { count++; } } export class a{ } }
Comments
Post a Comment