
These are the two lines of code extracted var name = Symbol('test') It keeps prompting that it cannot be converted. Is the keyword reserved? Or other reasons?
Why can it be compiled by changing var name1 = Symbol('test')?
Other ordinary var s1 s2 can also be compiled.
This question is quite interesting. I have never noticed it. After looking through the information, I found that many things came together by coincidence, and then this problem appeared. To be precise, it’s the
browser’s default behaviorand JavaScript’simplicit type conversionthat’s causing the trouble.Let’s go step by step, first of all, what is the difference between
The variable declared byvarandlet?varwill be promoted to the top of the current function scope. If it is global, then this variable will become an attribute ofwindow.For variables declared by
let, it will promote the variable to the current block-level scope, and if it is global, the current variable will not become an attribute ofwindow.So, in the big picture something like this happens:
Then, what is the difference between the variable named
nameand other variables?We know above that
var name = 'test1';can actually be equivalent towindow.name = 'test1'. It is easy to think, isnamea fixed reserved word?Looking through the specifications, it’s true. The
Thewindow.nameattribute represents the name of the current window context. The following are some interfaces ofwindow:nameattribute in the last line here does not have the prefixreadonly, indicating that it is readable and writable, and its data type isDOMString.DOMStringrefers to a UTF-16 string, which is directly mapped toStringin JavaScript.So when we assign a value to
window.name, this value will be coerced intoString.We can give it a try:
You can probably guess at this point that the error in
var name = Symbol('test');should be that there is a problem with the type conversion of theSymbolvariable. The actual error reported also confirmed our guess:TypeError: Cannot convert a Symbol value to a string.However, it doesn’t seem right.
Symbolvariables can be converted into strings, for example:Well, this is a cliché. JavaScript’s implicit type conversion and explicit cast are different for some variables. Unfortunately,
WhenSymbolfalls into this category here.Symbolis implicitly converted, it will first call its internalToPrimitiveinterface to get its original value, and then call theToStringfunction to convert it to a string. Note that theToStringfunction here is its internal abstract method, and is not the same thing as the exposedSymbol.prototype.toString().For the
Symbolvariable, an error will be reported when it callsToString. I will not go into more details. If you are interested, you can read the specification for yourself: ToString (argument).I just tried it on the console, it is indeed a very amazing BUG, but you will
Then I was surprised to find that the BUG was gone again. . I guess it has something to do with how the browser parses syntax, but I don't understand these things.
name is a unique attribute of window. If you try changing the variable, you will not get an error. . .
name is a unique attribute of window. Any value assigned to the name variable in the global environment will be automatically converted into a string. However, the Symbol type cannot be directly converted into a string, so an error is reported.
You can
You’ll know.