2

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

deceze
  • 510,633
  • 85
  • 743
  • 889

2 Answers2

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