我可以将变量设置为undefined或将undefined作为参数传递吗?

本文翻译自: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的undefinednull值有点困惑。

What does if (!testvar) actually do? if (!testvar)实际上做了什么? Does it test for undefined and null or just undefined ? 它是测试undefinednull还是只是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. 所以null0falseNaN"" (空字符串)都将显示为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. 根据定义,不应将nullundefined计算为truefalse ,但JavaScript会将null计算为false ,如果尝试计算未定义的变量,则会给出错误。

To properly test for undefined or null , use these: 要正确测试undefinednull ,请使用以下命令:

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. 您可以省略函数调用中的参数以使它们未定义,但只能通过向函数发送较少的参数来使用,您不能在中间省略参数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值