uniqueId
Available since v0.6
The uniqueId()
is able to return a "unique¹" reference identifier for any given object.
import { uniqueId, hasUniqueId } from "@aedart/support/objects";
const target = {
name: 'Ursula'
};
console.log(uniqueId(target)); // 27
// ...later in your application
console.log(hasUniqueId(target)); // true
console.log(uniqueId(target)); // 27
The source code is heavily inspired by Nicolas Gehlert's blog post: "Get object reference IDs in JavaScript/TypeScript" (September 28, 2022)
¹: In this context, the returned number is unique in the current session. The number will NOT be unique across multiple sessions, nor guarantee that an object will receive the exact same identifier as in a previous session!