What is the global variable called 'name' in javascript? [duplicate]

左心房为你撑大大i 提交于 2019-12-18 09:23:23

问题


Why is it that assigning a DOM element to the global variable "name" doesn't work ?


回答1:


Most "globals" in JavaScript when running in a browser are actually properties of the window object (of type Window).

But Window already has a name property, so any attempt to assign a non-string to it is going to lead to conversion to a string: the type of the assigned object will not be maintained.




回答2:


While name by itself is a property of the window object, you may still use name with another object as follows:

var obj2 = { "s1": "spring", "s2": "summer", "s3": "fall", "s4":"winter"};

obj2.name = obj2["s4"];
console.log("Name: " + obj2.name);

obj2.favoriteWeather = obj2.name;
console.log("Favorite season: " + obj2.favoriteWeather);

Resource: MDN Talk: Reserved_Words



来源:https://stackoverflow.com/questions/41652579/what-is-the-global-variable-called-name-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!