5

The objects I use on the client side need a unique ID. My array of objects is very small, commonly 4 items, not more.

I use this:

export class MyItem {   
    uniqueObjectIdentifier: number;
    constructor(obj) {    
        this.uniqueObjectIdentifier = Math.random();
    }
}

Is there something maybe javascript or angular internal that I can access instead of the handcrafted property?

cs95
  • 379,657
  • 97
  • 704
  • 746
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
  • Not flagging because I don't know enough about `angular`, `typescript`, or `ecmascript-6`, but is this the same question as [this](https://stackoverflow.com/q/1997661/1586231)? – Max von Hippel Jul 12 '17 at 18:33
  • What is wrong with `Math.random()` ? – bhantol Jul 12 '17 at 18:43
  • 1
    `Math.random()` is not unique. In javascript it's actually pseudo random, so it will repeat itself eventually. – Ori Drori Jul 12 '17 at 18:50
  • What do you want to use the id for? Why do you think you need them? Every object already has an identity that distinguishes it from others. – Bergi Jul 12 '17 at 23:32
  • Here I reas as solution: https://stackoverflow.com/questions/2020670/javascript-object-id that javascript has no built in identifier. I created objects on client side for a data grid. The one object currently edited shows the edit template for this I need the unique id :-) – Elisabeth Jul 13 '17 at 09:21

2 Answers2

7

You can use a Symbol.

Every symbol value returned from Symbol() is unique.

export class MyItem {
  uniqueObjectIdentifier: symbol;
  constructor(obj) {
    this.uniqueObjectIdentifier = Symbol();
  }
}

If browser support doesn't allow you to use a symbol, you can use a reference to an object instead.

export class MyItem {
  uniqueObjectIdentifier: object;
  constructor(obj) {
    this.uniqueObjectIdentifier = {};
  }
}
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • Wondering how is `Symbol()` or `{}` different from the actual `var item1 = new MyItem(); item2 = new MyItem()` – bhantol Jul 12 '17 at 18:44
  • as you asked before bhantol, nothing wrong with Math.random(). Just curious how a decade later... such stuff is handled :-) – Elisabeth Jul 12 '17 at 18:48
  • 1
    > A symbol value may be used as an identifier for object properties. There are built in special symbols, such as iterator. In addition, the purpose of a symbol is to be a unique identifier. The purpose of an object is to be an object. In this regard, there's not difference between `{}` and `new MyItem()`. – Ori Drori Jul 12 '17 at 18:49
  • 1
    @bhantol A `Symbol` is `Object` like, yet much simpler than a real `Object`. For instance, you can't assign a custom property to a `Symbol`. So it probably requires less memory. –  Jul 12 '17 at 18:51
  • Very nice `Symbol`. Hope IE soon starts supporting. – bhantol Jul 12 '17 at 18:54
  • 3
    and Math.random() purpose is a random number, not to be unique ;-) – Elisabeth Jul 12 '17 at 18:55
  • 1
    Why is browser support a problem at all? This can be fixed with Symbol polyfill, and polyfills are first class citizens in every web project. – Estus Flask Jul 12 '17 at 19:04
  • Not a big problem. I like poly fills rather than clutter my code. Already liking Symbol – bhantol Jul 12 '17 at 19:19
2

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 unexported, module scoped variable to hold the counter:

let currentId = 0;

export class MyItem {
  constructor(obj) {
    this.uniqueObjectIdentifier = currentId++;
  }
}

That way currentId can't be modified from outside the module.

TeWu
  • 5,928
  • 2
  • 22
  • 36