In TypeScript, you can do this:
export class MyItem { static currentId: number = 0; uniqueObjectIdentifier: number; constructor(obj) { this.uniqueObjectIdentifier = MyItem.currentId++; } }
In JavaScript ES6:
export class MyItem { constructor(obj) { this.uniqueObjectIdentifier = MyItem.currentId++; } } MyItem.currentId = 0;
Or you can use an extended variable with scope to hold a counter:
let currentId = 0; export class MyItem { constructor(obj) { this.uniqueObjectIdentifier = currentId++; } }
Thus, currentId cannot be changed from outside the module.
source share