javascript 初始化为 undefined 或 null 或 ""
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6072140/
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
javascript initialize to undefined or null or ""
提问by Kiran
Java script has many falsy values as I started learning. I have a program that gets values from a service and loads into an array like this:
在我开始学习时,Java 脚本有很多错误值。我有一个程序可以从服务中获取值并加载到这样的数组中:
function loadNames() {
Global.names = // what should I use here? undefined, null, "", 0, {} or anything else
var lnames = getLNames(); // this is doing some magic
if ( lnames.length !== 0 ) {
Global.names = new Array();
for ( var i = 0; i < lnames.length; ++i)
Global.names[i] = lnames[i];
}
}
I want to know the right way of resetting Global.names. What is most appropriate here? In code I only want to check like if ( Global.names )
我想知道重置 Global.names 的正确方法。这里什么最合适?在代码中,我只想检查if ( Global.names )
PS: I can't just take the return value into Global.names as the returned object is destroyed later. Hence, I need to do a deep copy
PS:我不能只是将返回值带入 Global.names 中,因为返回的对象稍后会被销毁。因此,我需要做一个深拷贝
Thanks
谢谢
回答by karlipoppins
Taken from JavaScript: the good parts :
取自 JavaScript:好的部分:
"The if statement changes the flow of the program based on the value of the expression. The then block is executed if the expression is truthy. Here are the falsy values:
“if 语句根据表达式的值改变程序的流程。如果表达式为真,则执行 then 块。以下是假值:
false
null
undefined
the empty string ''
the number 0
the number NaN "
错误的
空值
不明确的
空字符串 ''
数字 0
数字 NaN "
So basically if you set your var to any of those values, you'll be able to do a if(var){...}
所以基本上,如果你将 var 设置为这些值中的任何一个,你就可以执行 if(var){...}
回答by Ionut Popa
I think you should init
GLobal.names = [];
and than just check if Global.names.length != 0
.
If you want to reset it just make it an empty array again.
我认为你应该初始化
GLobal.names = [];
,而不仅仅是检查是否Global.names.length != 0
. 如果要重置它,只需再次将其设为空数组即可。
回答by ic3b3rg
I think you'd be better off to initialize it as an array and test as
我认为你最好将它初始化为一个数组并测试为
if ( Global.names.length )
Also, if you're just storing strings in the array you can simplify the function as
此外,如果您只是在数组中存储字符串,则可以将函数简化为
function loadNames() {
Global.names = getLNames().concat();
}
回答by RobG
You don't have to initialise it to anything. You can do:
您不必将其初始化为任何内容。你可以做:
if (!Global.names) Global.names = [];
// add members to Global.names
This can be one in one statement:
这可以是一对一的声明:
Global.names = Global.names : [];
If you want to reset it, then:
如果你想重置它,那么:
Global.names = [];
or
或者
delete Global.names;
回答by chrisf
Setting it to null
is good, as you know that the variable exists but hasn't been assigned a value. This way you can easily see the state - if it's undefined
then you've forgotten to declare it, if it's null
it has been declared but never assigned a value, and if it's an Array
then you can test the length from there.
将其设置null
为很好,因为您知道该变量存在但尚未分配值。这样你就可以很容易地看到状态——如果是,undefined
那么你忘记声明它,如果它null
已经声明但从未分配过值,如果它是一个,Array
那么你可以从那里测试长度。