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 behavior
and JavaScript’simplicit type conversion
that’s causing the trouble.Let’s go step by step, first of all, what is the difference between
The variable declared byvar
andlet
?var
will 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
name
and other variables?We know above that
var name = 'test1';
can actually be equivalent towindow.name = 'test1'
. It is easy to think, isname
a fixed reserved word?Looking through the specifications, it’s true. The
Thewindow.name
attribute represents the name of the current window context. The following are some interfaces ofwindow
:name
attribute in the last line here does not have the prefixreadonly
, indicating that it is readable and writable, and its data type isDOMString
.DOMString
refers to a UTF-16 string, which is directly mapped toString
in 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 theSymbol
variable. The actual error reported also confirmed our guess:TypeError: Cannot convert a Symbol value to a string
.However, it doesn’t seem right.
Symbol
variables 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,
WhenSymbol
falls into this category here.Symbol
is implicitly converted, it will first call its internalToPrimitive
interface to get its original value, and then call theToString
function to convert it to a string. Note that theToString
function here is its internal abstract method, and is not the same thing as the exposedSymbol.prototype.toString()
.For the
Symbol
variable, 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.