Why is it that assigning a DOM element to the global variable "name
" doesn't work ?
Asked
Active
Viewed 1,790 times
2

deceze
- 510,633
- 85
- 743
- 889

Sanjay Josh
- 87
- 6
-
Please read through [ask] and [mcve] – charlietfl Jan 14 '17 at 17:26
2 Answers
2
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.

Richard
- 106,783
- 21
- 203
- 265
0
In the global space there is only one name property possible which is that of the Window object and you can only assign a text string as its value. The purpose of this global variable associated with the Window object is to set targets for hyperlinks and forms. However, you may create a property called 'name' with another object as long as it originates from where the object is created, as follows:
function season(name, starts, equinox) {
this.name = name;
this.starts = starts;
this.equinox=equinox;
};
function demoObj(favSeason,presDay) {
this.favSeason = favSeason;
this.presDay = presDay;
};
const seasonNow = new season('Spring','March','Vernal');
const o = new demoObj(seasonNow,'2-20-2022');
console.log("Favorite season: " + o.favSeason.name + " [as of " + o.presDay+ "]");
More info here

slevy1
- 3,797
- 2
- 27
- 33
-
1
-
1@leo848 Read the [documentation](//developer.mozilla.org/docs/Web/API/Window/name). – Sebastian Simon Feb 16 '22 at 07:51
-
@leo848 I just slightly modified my answer for greater clarity which should prove instructive. – slevy1 Feb 22 '22 at 06:48