liveBook · Manning

Namespace collision

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

An accidental conflict caused when distinct identifiers or values are mistaken for one another or map to the same name.

What if a tooltip expects a position flag, but receives an answer flag whose value is also "right"? The function can mistake one identifier for the other. A namespace collision occurs when distinct names or values are confused; JavaScript Symbols can provide unique flags. section 13.1

Listing 13.1. Using constants as flags
const answer = { CORRECT : 'right', INCORRECT : 'wrong' } const positions = { TOP : 'top', BOTTOM : 'bottom', LEFT : 'left', RIGHT : 'right' } function addToolTip(content, position) { switch(position) { case positions.TOP: // add content above break; case positions.BOTTOM: // add content below break; case positions.RIGHT: 1 // add content to right break; case positions.LEFT: // add content to left break; default: throw new Error(`${position} is not a valid position`) break; } } addToolTip('You are not logged in', answer.CORRECT); 2

The answer and position objects use string values, so passing answer.CORRECT matches positions.RIGHT and selects the right-side case instead of reporting an error. section 13.1

Give each flag a unique Symbol

Set positions.RIGHT to Symbol('right'). Calling Symbol('right') again creates a different symbol, so a flag from another set will not match it merely because the descriptions are the same. section 13.1

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Explains the flag-confusion scenario and Symbol uniqueness, without detailing broader symbol behavior.
Examples
Uses tooltip position flags that accidentally match answer flags, then contrasts repeated Symbol descriptions.
What it assumes
Assumes familiarity with object-based flags and switch cases; does not teach those constructs from zero.
What you can do after
Apply Symbol() to create unique flags that avoid accidental matches.
How it connects
Connects namespace collisions with JavaScript Symbols, constants, and action handlers.

Also explained in

Compare how 3 books explain namespace collision →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.