admin管理员组文章数量:1302545
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)
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)
Share asked Oct 25, 2014 at 13:07 MultisyncMultisync 8,7971 gold badge19 silver badges20 bronze badges 2- See this: developer.mozilla/en-US/docs/Web/API/Window.name – friedi Commented Oct 25, 2014 at 13:09
- +1 for testing this right. – djechlin Commented Oct 25, 2014 at 13:19
2 Answers
Reset to default 12Is 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 windowtitle
- The title of the current windowstatus
- The status area content (except most browsers ignore it)document
- The document in the current windowwindow
- 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.
name
is a property of window
notName
is not, until it is defined as such
var output = "window.name: " + window.name + "\r\n" + "name: " + name;
alert(output);
本文标签: Is variable called quotnamequot always defined in JavascriptStack Overflow
版权声明:本文标题:Is variable called "name" always defined in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741664216a2391211.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论