本文翻译自:Can I set variables to undefined or pass undefined as an argument?
I'm a bit confused about JavaScript's undefined
and null
values. 我对JavaScript的undefined
和null
值有点困惑。
What does if (!testvar)
actually do? if (!testvar)
实际上做了什么? Does it test for undefined
and null
or just undefined
? 它是测试undefined
和null
还是只是undefined
?
Once a variable is defined can I clear it back to undefined
(therefore deleting the variable)? 一旦定义了变量,我可以将其清除为undefined
(因此删除变量)吗?
Can I pass undefined
as a parameter? 我可以将undefined
作为参数传递吗? Eg: 例如:
function test(var1, var2, var3) {
}
test("value1", undefined, "value2");
#1楼
参考:https://stackoom.com/question/9NaQ/我可以将变量设置为undefined或将undefined作为参数传递吗
#2楼
JavaScript, how to set a variable to undefined on commandline: JavaScript,如何在命令行上将变量设置为undefined:
Set a variable to undefined in the js
javascript command line terminal that comes with Java on Ubuntu 12.10. 在Ubuntu 12.10上的Java附带的js
javascript命令行终端中将变量设置为undefined。
el@defiant ~ $ js
js> typeof boo
"undefined"
js> boo
typein:2: ReferenceError: boo is not defined
js> boo=5
5
js> typeof boo
"number"
js> delete(boo)
true
js> typeof boo
"undefined"
js> boo
typein:7: ReferenceError: boo is not defined
If you set a variable to undefined in a javascript: 如果在javascript中将变量设置为undefined:
Put this in myjs.html: 把它放在myjs.html中:
<html>
<body>
<script type="text/JavaScript">
document.write("aliens: " + aliens);
document.write("typeof aliens: " + (typeof aliens));
var aliens = "scramble the nimitz";
document.write("found some aliens: " + (typeof aliens));
document.write("not sayings its aliens but... " + aliens);
aliens = undefined;
document.write("aliens deleted");
document.write("typeof aliens: " + (typeof aliens));
document.write("you sure they are gone? " + aliens);
</script>
</body>
</html>
It prints this: 它打印这个:
aliens: undefined
typeof aliens: undefined
found some aliens: string
not sayings its aliens but... scramble the nimitz
aliens deleted
typeof aliens: undefined
you sure they are gone? undefined
WARNING! 警告! When setting your variable to undefined you are setting your variable to another variable. 将变量设置为undefined时,您将变量设置为另一个变量。 If some sneaky person runs undefined = 'rm -rf /';
如果一些偷偷摸摸的人跑了undefined = 'rm -rf /';
then whenever you set your variable to undefined, you will receive that value. 然后,每当您将变量设置为undefined时,您将收到该值。
You may be wondering how I can output the undefined value aliens at the start and have it still run. 您可能想知道如何在开始时输出未定义的值外星人并让它仍然运行。 It's because of javascript hoisting: http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html 这是因为javascript悬挂: http : //www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html
#3楼
To answer your first question, the not operator ( !
) will coerce whatever it is given into a boolean value. 为了回答你的第一个问题,not运算符( !
)将强制赋予它一个布尔值。 So null
, 0
, false
, NaN
and ""
(empty string) will all appear false. 所以null
, 0
, false
, NaN
和""
(空字符串)都将显示为false。
#4楼
The best way to check for a null values is 检查空值的最佳方法是
if ( testVar !== null )
{
// do action here
}
and for undefined 并且未定义
if ( testVar !== undefined )
{
// do action here
}
You can assign a avariable with undefined. 你可以指定一个未定义的avariable。
testVar = undefined;
//typeof(testVar) will be equal to undefined.
#5楼
You cannot (should not?) define anything as undefined, as the variable would no longer be undefined – you just defined it to something. 你不能(不应该?)定义任何未定义的东西,因为变量将不再是未定义的 - 你只是将它定义为某种东西。
You cannot (should not?) pass undefined
to a function. 你不能(不应该?)将undefined
传递给一个函数。 If you want to pass an empty value, use null
instead. 如果要传递空值,请改用null
。
The statement if(!testvar)
checks for boolean true/false values, this particular one tests whether testvar
evaluates to false
. 语句if(!testvar)
检查布尔值true / false值,这个特定的值测试testvar
是否计算为false
。 By definition, null
and undefined
shouldn't be evaluated neither as true
or false
, but JavaScript evaluates null
as false
, and gives an error if you try to evaluate an undefined variable. 根据定义,不应将null
和undefined
计算为true
或false
,但JavaScript会将null
计算为false
,如果尝试计算未定义的变量,则会给出错误。
To properly test for undefined
or null
, use these: 要正确测试undefined
或null
,请使用以下命令:
if(typeof(testvar) === "undefined") { ... }
if(testvar === null) { ... }
#6楼
The for if (something)
and if (!something)
is commonly used to check if something is defined or not defined. for if (something)
和if (!something)
通常用于检查是否定义了某些内容。 For example: 例如:
if (document.getElementById)
The identifier is converted to a boolean value, so undefined
is interpreted as false
. 标识符将转换为布尔值,因此undefined
被解释为false
。 There are of course other values (like 0 and '') that also are interpreted as false
, but either the identifier should not reasonably have such a value or you are happy with treating such a value the same as undefined. 当然还有其他值(如0和'')也被解释为false
,但是标识符不应该合理地具有这样的值,或者您很乐意将这样的值与undefined相同。
Javascript has a delete
operator that can be used to delete a member of an object. Javascript有一个delete
操作符,可用于删除对象的成员。 Depending on the scope of a variable (ie if it's global or not) you can delete it to make it undefined. 根据变量的范围(即,如果它是全局的),您可以删除它以使其未定义。
There is no undefined
keyword that you can use as an undefined literal. 没有undefined
关键字可用作未定义的文字。 You can omit parameters in a function call to make them undefined, but that can only be used by sending less paramters to the function, you can't omit a parameter in the middle. 您可以省略函数调用中的参数以使它们未定义,但只能通过向函数发送较少的参数来使用,您不能在中间省略参数。