How to convert a String to a Symbol in JavaScript
问题 I need to find a way to convert any string to a symbol. If there was a function that did that, it would be something like this: function toSymbol(variable) = { //... converts var to symbol }; //toSymbol("mySymbolString") would return: mySymbolString Is there any clever way of doing this other than storing potential string to symbol mappings in a dictionary? 回答1: function toSymbol(variable) { return Symbol(variable); }; Keep in mind toSymbol("some_string") === toSymbol("some_string") // false