What is the global variable called 'name'

2019-01-29 03:39发布

This question already has an answer here:

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

2条回答
forever°为你锁心
2楼-- · 2019-01-29 04:29

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

查看更多
姐就是有狂的资本
3楼-- · 2019-01-29 04:41

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.

查看更多
登录 后发表回答