<\/script>')

Is a variable called "name" always defined in Javascript?

document.writeln('name=' + name); // name = document.writeln('notName=' + notName); // ReferenceError: notName is not defined 

Does "name" have any special meaning in javascript? (tested in IE and FF)

+5
source share
2 answers

Is a variable called "name" always defined in Javascript?

Not. However, browsers have a global name , which is the name of the current window. This is a byproduct of the fact that the global JavaScript object in browsers is a Window object. A little explanation:

In JavaScript, global variables are actually properties of what is called a global object. In browsers, a global object is a Window object for a page, and therefore it has all sorts of predefined properties (and therefore global) on it, associated with it as a Window object , including, but not limited to:

  • name - name of the current window
  • title - name of the current window
  • document - document in the current window
  • window - a reference to a global object (for example, a circular link)
  • setTimeout is a function used to schedule what happens later.

... and many others. It also gets all sorts of other things into it, such as a property for each DOM element with id (the name of the property is id , its value is a reference to the DOM element), on some browsers the same is true for DOM elements with the name property, etc. . He was very clogged.

+10
source

name is a window property

notName not until defined as such

 var output = "window.name: " + window.name + "\r\n" + "name: " + name; alert(output); 
+3
source

Source: https://habr.com/ru/post/1205495/


All Articles