简体   繁体   English

没有初始值的JavaScript变量在一种情况下返回null,而在另一种情况下未定义

[英]JavaScript variable with no initial value returns null in one case and undefined in another

So I am learning JavaScript and tested the code below: 因此,我正在学习JavaScript并测试了以下代码:

var name;
console.log( name );

returns null 返回null

var c;
console.log( c ); 

returns undefined 返回undefined

why is this? 为什么是这样?

Documentation states: A variable declared using the var statement with no initial value specified has the value undefined. 文档状态: A variable declared using the var statement with no initial value specified has the value undefined.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Declaring_variables https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Declaring_variables

Any references to official documentation would be great! 任何对官方文档的引用都很棒!

If you're doing this in a browser, it's because name already exists in the browser environment (it's the name of the window). 如果在浏览器中执行此操作,这是因为name 已经存在于浏览器环境中(这是窗口的名称)。 So the var name is a no-op. 因此var name是no-op。 But see below, the null bit surprises me. 但请参阅下文, null位让我感到惊讶。

In JavaScript, global variables are actually properties of the global object. 在JavaScript中,全局变量实际上是全局对象的属性。 The global object is the value this has in code at global scope*. 全局对象是值this在全球范围内的代码*。 In browsers, the global object is the browser's window object for that page, and so it has any properties defined for the Window object. 在浏览器中,全局对象是该页面的浏览器的window对象,因此它具有为Window对象定义的任何属性。 One of those is name . name (Another is window , which is a property the global object has that has a reference back to the global object, so you can use window to refer to it even not at global scope.) The global object on browsers is actually quite crowded, as browsers dump properties on it for any element that has an id (the property name is the id , the value is a reference to the element), there's forms , all on some browsers, document , and a bunch of other stuff. (另一个是window ,这是全局对象具有的属性,该属性具有对全局对象的引用,因此,即使不是在全局范围内,也可以使用window来引用它。)实际上,浏览器上的全局对象非常拥挤,因为浏览器倾倒在它的属性对于具有任何元素id (属性名是id ,该值是对元素的引用),有formsall在某些浏览器, document ,和一堆其他的东西。

Outside of browsers, the global object doesn't have any automatic property pointing to it (well, unless the environment adds one), and won't have browser-specific things on it (but may have things specific to whatever environment you're in). 在浏览器之外,全局对象没有指向它的任何自动属性(很好,除非环境添加了一个),并且它上没有浏览器特定的东西(但可能与您所处的任何环境都有特定的东西)在)。


* Unless you have "use strict" at the top of your script, which puts it in strict mode, in which case this is undefined . *除非你有"use strict"在你的脚本,这使它在严格模式,在这种情况下的顶部thisundefined


About you getting null as output from your first example: I find that very surprising; 关于您在第一个示例中获得null作为输出:我发现这非常令人惊讶; I would have expected "" (a blank string) or, of course, the name of the window if there is one. 我本来希望"" (一个空白字符串),或者,如果有的话,当然是窗口的名称。 Something, somewhere, must be assigning null or "null" to name (either of which make it the string "null" on the various browsers I've tried [Chrome, Firefox, IE8, IE11]). 必须在某处某处为name分配null"null" (在我尝试过的各种浏览器中,都可以将其设置为字符串"null" [Chrome,Firefox,IE8,IE11])。 You can try it with this page (jsFiddle and such give the window they run things in names, so I can't use them for this example): 您可以在此页面上尝试(jsFiddle等为窗口提供名称运行的窗口,因此在此示例中无法使用它们):

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
</head>
<body>
<script>
  (function() {
    "use strict";

    display("[before] typeof name: " + typeof name);
    display("[before] name: " + name);

    display("Doing <code>name = null;</code>");
    name = null; // Gets coerced to string "null"

    display("[after] typeof name: " + typeof name);
    display("[after] name: " + name);

    function display(msg) {
      var p = document.createElement('p');
      p.innerHTML = String(msg);
      document.body.appendChild(p);
    }
  })();
</script>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM