为什么人们在 JavaScript 中将某些内容声明为 null?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5613738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:45:50  来源:igfitidea点击:

Why do people declare something as null in JavaScript?

javascript

提问by 0x499602D2

Possible Duplicate:
Why is there a nullvalue in JavaScript?

可能的重复:
为什么nullJavaScript 中有一个值?

I don't understand what null is for. 'Undefined' as well.

我不明白 null 是什么意思。'未定义'也是如此。

回答by Rodrick Chapman

It helps to appreciate the difference between a value and a variable.

它有助于理解值和变量之间的差异。

A variable is a storage location that contains a value.

变量是包含值的存储位置。

nullmeans that the storage location does not contain anything (but null itself is just a special kind of value). Read that last sentence carefully. There is a difference between what nullis and what nullmeans. 99% of the time you only care about what null means.

null意味着存储位置不包含任何内容(但 null 本身只是一种特殊的值)。仔细阅读最后一句话。是什么nullnull意味着什么是有区别的。99% 的情况下,您只关心 null 的含义。

Undefinedmeans that the variable(as opposed to the value) does not exist.

Undefined意味着变量(相对于value)不存在。

回答by typeof

nullis an object you can use when creating variables when you don't have a value yet:

null是一个对象,当您还没有值时,您可以在创建变量时使用它:

var myVal = null;

When you do this, you can also use it to check to see if it's defined:

执行此操作时,您还可以使用它来检查它是否已定义:

// null is a falsy value
if(!myVal) {
    myVal = 'some value';
}

I wouldn't use it this way, normally. It's simple enough to use 'undefined', instead:

正常情况下,我不会以这种方式使用它。使用“未定义”很简单,而是:

var myVal; // this is undefined

And it still works as a falsy value.

它仍然可以作为一个假值。

Creating Objects

创建对象

When you create an object in javascript, I like to declare all my object properties at the top of my object function:

当你在 javascript 中创建一个对象时,我喜欢在我的对象函数的顶部声明我所有的对象属性:

function myObject() {
    // declare public object properties
    this.myProp = null;
    this.myProp2 = null;

    this.init = function() {
        // ... instantiate all object properties
    };
    this.init();
}

I do this because it's easier to see what the object properties are right off the bat.

我这样做是因为更容易立即查看对象属性。

回答by Alex

If a variable is not known or not initialized in the current scope it is undefined.

如果变量在当前作用域中未知或未初始化,则为undefined.

If you want to use a variable and indicate that it has an invalid value for instance you may want to assign it nullbecause accessing an undefined variable will throw an error if you try to work with it (despite checking if it is undefined).

如果你想使用一个变量并指出它有一个无效的值,例如你可能想要分配它,null因为如果你尝试使用它访问一个未定义的变量会抛出一个错误(尽管检查它是否未定义)。

If something is undefined you also have the possibility to add it to an object. If a variable is defined but contains null you know that this variable may be used already by another part of your program.

如果某些内容未定义,您也可以将其添加到对象中。如果定义了一个变量但包含 null,则您知道该变量可能已被程序的另一部分使用。

回答by Richard Rodriguez

For example, it can prepare a global variable to be used in more parts of the code.

例如,它可以准备一个全局变量,以便在代码的更多部分中使用。

If you start your code with var iNeedThisEverywhere = null;then you can use it inside more objects/actions in the code, but if you don't do that, then this variable will not be shared throughout the code.

如果你开始你的代码,var iNeedThisEverywhere = null;那么你可以在代码中的更多对象/动作中使用它,但如果你不这样做,那么这个变量将不会在整个代码中共享。

Of course, you will actually put something inside that variable somewhere during the code, but since you defined this variable at the very beginning, it will be shared further anyway.

当然,您实际上会在代码中的某个地方放入该变量中的某些内容,但是由于您在一开始就定义了该变量,所以无论如何它都会被进一步共享。

回答by Teson

A variable holding null holds a reference to the "black hole" named null.

持有 null 的变量持有对名为 null 的“黑洞”的引用。

10 + 10 + null = null

Null is related historically to databases, http://en.wikipedia.org/wiki/Null_(SQL) (Correct me if I'm wrong here)

Null 在历史上与数据库有关,http://en.wikipedia.org/wiki/Null_( SQL )(如果我在这里错了,请纠正我)

A variable not initiated (unknown) is undefined.

未启动(未知)的变量未定义。

regards, /t

问候,/t