ES6:为什么 Symbol 不能有另一个符号作为描述?

ES6: Why Symbol can not have another symbol as description?

我正在学习 Es6:Symbols。我正在玩它并尝试使用另一个符号作为描述来创建一个符号:

var s = Symbol('foo');
console.log(s.toString()); //"Symbol(foo)"
var sS = Symbol(s); // <- thorws error
var sF = Symbol.for(s); // <- thorws error
console.log(s, sA);

我无法理解为什么不让我使用现有符号作为描述。当我 运行 上面的代码时,我看到以下控制台错误:

Uncaught TypeError: Cannot convert a Symbol value to a string
    at Function.for (native)

正如错误所说,它无法将符号转换为字符串。但是正如您在代码(第 2 行)中看到的那样,通过使用 toString() 函数,我能够将符号转换为字符串。任何人都可以详细说明发生了什么事吗?谢谢

why is it not letting me use existing symbol as description

仅仅是因为所有的描述都必须是字符串,没有别的。

the error says that it can not convert the symbol to string, but by using toString() I am able to convert the symbol in string

是的,您可以显式 将符号转换为字符串并使用toString 方法获取其描述。但是所有隐式转换都会抛出异常——这是一个 deliberate feature 来防止我们不小心使用 non-unique 描述而不是符号,例如使用 属性 键进行字符串连接时。