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?

5
  • Not flagging because I don't know enough about angular, typescript, or ecmascript-6, but is this the same question as this? Commented Jul 12, 2017 at 18:33
  • What is wrong with Math.random() ? Commented Jul 12, 2017 at 18:43
  • 1
    Math.random() is not unique. In javascript it's actually pseudo random, so it will repeat itself eventually. Commented Jul 12, 2017 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. Commented Jul 12, 2017 at 23:32
  • Here I reas as solution: 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 :-) Commented Jul 13, 2017 at 9:21

2 Answers 2

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 = {};
  }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Wondering how is Symbol() or {} different from the actual var item1 = new MyItem(); item2 = new MyItem()
> 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().
@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.
and Math.random() purpose is a random number, not to be unique ;-)
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.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.