diff --git a/src/cdk/a11y/id-generator.ts b/src/cdk/a11y/id-generator.ts index 8d75a7d0f50c..58856ecc22f7 100644 --- a/src/cdk/a11y/id-generator.ts +++ b/src/cdk/a11y/id-generator.ts @@ -12,8 +12,10 @@ import {APP_ID, inject, Service} from '@angular/core'; * Keeps track of the ID count per prefix. This helps us make the IDs a bit more deterministic * like they were before the service was introduced. Note that ideally we wouldn't have to do * this, but there are some internal tests that rely on the IDs. + * + * Note: use a map to avoid conflicts with built-in properties. */ -const counters: Record = {}; +const counters = new Map(); /** Service that generates unique IDs for DOM nodes. */ @Service() @@ -33,10 +35,15 @@ export class _IdGenerator { prefix += this._appId; } - if (!counters.hasOwnProperty(prefix)) { - counters[prefix] = 0; + let count = counters.get(prefix); + + if (count === undefined) { + count = 0; + } else { + count++; } - return `${prefix}${randomize ? _IdGenerator._infix + '-' : ''}${counters[prefix]++}`; + counters.set(prefix, count); + return `${prefix}${randomize ? _IdGenerator._infix + '-' : ''}${count}`; } }