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

  1. declare variable in module scope

    module m {   var count : number = 0;   export class c {     constructor() {       count++;     }   } } 
  2. 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

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -