Why are symbols not frozen strings?

前端 未结 6 1909
广开言路
广开言路 2021-01-31 17:59

I understand the theoretical difference between Strings and Symbols. I understand that Symbols are meant to represent a concept or a name or an identifier or a label or a key, a

6条回答
  •  囚心锁ツ
    2021-01-31 18:26

    If at all a String could inherit Symbol, because it adds a lot of functionality (mutating). But a Symbol can never be used "as a" String because in all circumstances where mutation would be needed it would fail.

    In any case, as I said above, string == symbol must never return true as has been suggested above. If you think a bit about this you'll notice that there can be no reasonable implementation of == in a class which considers sub class instances as well.

提交回复
热议问题