Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

Is variable called "name" always defined in Javascript?

document.writeln('name=' + name); 
// name =

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

Does the "name" have some special meaning in javascript? (checked in IE and FF)

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Is variable called “name” always defined in Javascript?

No. However, on browsers there's a global called name which is the name of the current window. This is a by-product of the fact that the JavaScript global object on browsers is the Window object. A bit of explanation:

In JavaScript, global variables are actually properties of something called the "global object." On browsers, the global object is the Window object for the page, and so it has all kinds of predefined properties (and therefore globals) on it related to it being a Window object, including but not limited to:

  • name - The name of the current window
  • title - The title of the current window
  • status - The status area content (except most browsers ignore it)
  • document - The document in the current window
  • window - A reference back to the global object (e.g., a circular reference)
  • setTimeout - A function used for scheduling something to happen later

...and many others. It also gets all kinds of other things thrown into it, such as a property for every DOM element that has an id (the property's name is the id, its value is a reference to the DOM element), on some browsers the same is true for DOM elements with a name property, and so on. It's very cluttered.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...