最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why, in ECMAScript, are symbols created with the same description not the same symbol, unlike most other languages

programmeradmin3浏览0评论

I've been around the block when it es to languages, having worked with everything from C# to Lisp to Scala to Haskell, and in every language that supported them, symbols have acted pretty much the same; that is, any two symbols with the same name, are guaranteed to be identical because they're singleton objects.

Racket: (equal? 'foo 'foo) true

Common Lisp: (eq 'foo 'foo) true

Ruby: :foo == :foo true

Scala: 'foo == 'foo true

ES6: Symbol('foo') === Symbol('foo') false

The benefit of symbols being singletons is obvious: You can use them in maps/dictionaries without risking your key not being equal to your input because the language suddenly decided to hash it differently (looking at you, Ruby)

So why is it that ECMAScript 6 takes a different approach on this, and how can I get around it?

I've been around the block when it es to languages, having worked with everything from C# to Lisp to Scala to Haskell, and in every language that supported them, symbols have acted pretty much the same; that is, any two symbols with the same name, are guaranteed to be identical because they're singleton objects.

Racket: (equal? 'foo 'foo) true

Common Lisp: (eq 'foo 'foo) true

Ruby: :foo == :foo true

Scala: 'foo == 'foo true

ES6: Symbol('foo') === Symbol('foo') false

The benefit of symbols being singletons is obvious: You can use them in maps/dictionaries without risking your key not being equal to your input because the language suddenly decided to hash it differently (looking at you, Ruby)

So why is it that ECMAScript 6 takes a different approach on this, and how can I get around it?

Share Improve this question edited Feb 28 at 16:56 dumbass 27.3k4 gold badges38 silver badges74 bronze badges asked Nov 22, 2015 at 15:31 Electric CoffeeElectric Coffee 12.1k10 gold badges73 silver badges139 bronze badges 1
  • ES6 symbols are like Lisp "uninterned" symbols. – Barmar Commented Feb 28 at 17:17
Add a ment  | 

1 Answer 1

Reset to default 15

You can (sort-of) get the effect of symbols being "knowable" by name by using registered (global) symbols:

var s1 = Symbol.for("foo");
var s2 = Symbol.for("foo");
s1 === s2; // true

Of course you can create your own Symbol registry too, with a Map instance or a plain object.

edit — I'll add that the intention of the optional string parameter when making a new Symbol instance is to provide a way of identifying the meaning and purpose of a symbol to the programmer. Without that string, a Symbol works just fine as a Symbol, but if you dump out an object in a debugger the properties keyed by such anonymous Symbol instances are just values. If you're keeping numeric properties on an object with Symbol keys, then you'll just see some numbers, and that would be confusing. The description string associated with a Symbol instances gives the programmer reference information without promising the uniqueness of the Symbol as a key value.

Finally, you can always pare the result of calling .toString() on two similarly-constructed Symbol instances. I suspect that that practice would be considered questionable, but you can certainly do it.

edit more — it occurs to me that the fact that the default behavior of Symbol creation in JavaScript makes the type more useful than, say, atoms in Erlang or keys in Clojure. Because the language provides by default a value guaranteed to be unique, the fundamental problem of namespace collision is solved pretty nicely. It's still possible to work with "well-known" symbols, but having unique values available without having to worry about conflicts with other code that might also want to avoid collisions is nice. JavaScript has the somewhat unique, and certainly uniquely pervasive and uncontrollable, problem of a global namespace that may be polluted by code that a programmer does not even know exists, because code can collide in the browser enviroment due to the actions of a party different from, and unknown to, an arbitrary number of software architects.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论