Inversifyjs: The Symbol's examples are misleading

Created on 19 May 2018  路  2Comments  路  Source: inversify/InversifyJS

Either the examples are misleading or I am misunderstanding.
As stated in:
https://github.com/inversify/InversifyJS/blob/e2cf550/wiki/symbols_as_id.md

In very large applications using strings as the identifiers of the types to be injected by the InversifyJS can lead to naming collisions.

In that case the Symbol should be used like:

Symbol("Ninja")

not

Symbol.for("Ninja")

This return false

Symbol("Ninja") === Symbol("Ninja")

but this return true

Symbol.for("Ninja") === Symbol.for("Ninja")

currently the for usage does not limited to just in the document.

The changes seem to be deliberate though:
https://github.com/inversify/InversifyJS/pull/708

Most helpful comment

Yes, please fix this.

This was my first proper encounter of Symbols and it was quite confusing to say the least.
I'm still not sure if there is any real difference between using "Ninja" vs. Symbol.for("Ninja").

To prevent naming conflicts you definitely have to use Symbol("Ninja").
The caveat of that solution is that you have to access the symbol definition from everywhere that you want to inject something, plus in your composition root.

All 2 comments

馃憤

A misunderstanding of how Symbols work doesn't mean that we should change examples to use something that has the potential for global collisions. Instead, we should encourage people to learn how Symbols work by linking to MDN documentation, examples, etc.

Enough people just straight copy and paste examples that they should be based on best practices with explanations about why it's being done in a particular way.

Yes, please fix this.

This was my first proper encounter of Symbols and it was quite confusing to say the least.
I'm still not sure if there is any real difference between using "Ninja" vs. Symbol.for("Ninja").

To prevent naming conflicts you definitely have to use Symbol("Ninja").
The caveat of that solution is that you have to access the symbol definition from everywhere that you want to inject something, plus in your composition root.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Deviad picture Deviad  路  3Comments

AlexanderKiriluyk picture AlexanderKiriluyk  路  4Comments

jshearer picture jshearer  路  4Comments

hexa00 picture hexa00  路  3Comments

RastriginSergey picture RastriginSergey  路  3Comments