Assign an object property of an object to a variable with the same name (browsers behave differently from node)

node

my chrome version is 63.0

excuse me, is this the bug of the browser? The same situation also appears in Firefox

Mar.16,2021

window.name

change name to a variable with a different name, or declare it with let .


window.name is an attribute of that already exists on the window object. This attribute is a string type . When you use var name = a.name , you are actually assigning a value to window.name .
because window.name only accepts values of type string, if the value is not of type string, it is cast to type string.
a.name is an object, so toString.call (a.name, a.name) is called, and the result is '[object Object]', so name becomes '[object Object]'.
in the node environment, name is not a property of the global object, so it can be set normally.

Menu