我可以将变量设置为undefined或将undefined作为参数传递吗? [英] Can I set variables to undefined or pass undefined as an argument?

查看:195
本文介绍了我可以将变量设置为undefined或将undefined作为参数传递吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript的 undefined null 值感到有点困惑。

I’m a bit confused about JavaScript’s undefined and null values.

if(!testvar)实际上是做什么的?它是否测试 undefined null 或只是 undefined

What does if (!testvar) actually do? Does it test for undefined and null or just undefined?

一旦定义了变量,我可以将其清除回 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? E.g.:

function test(var1, var2, var3) {

}

test("value1", undefined, "value2");


推荐答案


我是一个有点困惑Javascript undefined& null。

I'm a bit confused about Javascript undefined & null.

不要对 null 感到困惑。它通常是有意义的,其行为类似于其他脚本语言的带外null,nil或None对象的概念。

Don't be confused about null. It generally makes sense and behaves similarly to other scripting languages' concepts of the out-of-band ‘null’, ‘nil’ or ‘None’ objects.

<$另一方面,c $ c> undefined 是一个奇怪的JavaScript怪癖。它是一个单独的对象,表示带外值,基本上是第二个相似但不同的 null 。它出现了:

undefined, on the other hand, is a weird JavaScript quirk. It's a singleton object that represents out-of-band values, essentially a second similar-but-different null. It comes up:


  1. 当你调用参数少于函数中的参数列表的函数时语句列表,未批处理的参数设置为 undefined 。您可以使用例如:。

  1. When you call a function with fewer arguments than the arguments list in the function statement lists, the unpassed arguments are set to undefined. You can test for that with eg.:

function dosomething(arg1, arg2) {
    if (arg2===undefined)
    arg2= DEFAULT_VALUE_FOR_ARG2;
    ...
}

使用这种方法你无法告诉 dosomething(1) dosomething(1,undefined)之间的差异; arg2 两者中的值相同。如果你需要区分你可以看看 arguments.length ,但是做这样的可选参数通常不太可读。

With this method you can't tell the difference between dosomething(1) and dosomething(1, undefined); arg2 will be the same value in both. If you need to tell the difference you can look at arguments.length, but doing optional arguments like that isn't generally very readable.

当函数没有返回值; 时,它返回 undefined 。通常不需要使用这样的返回结果。

When a function has no return value;, it returns undefined. There's generally no need to use such a return result.

通过在块中包含 var a 语句来声明变量时,但是没有但是为它分配了一个值,它是 undefined 。同样,你真的不需要依赖它。

When you declare a variable by having a var a statement in a block, but haven't yet assigned a value to it, it is undefined. Again, you shouldn't really ever need to rely on that.

怪异的 typeof 运算符返回'undefined'当它的操作数是一个不存在的简单变量时,而不是像你试图引用它时通常会发生的那样抛出错误。 (你也可以给它一个包含在括号中的简单变量,但一个涉及不存在的变量的完整表达式。)也没用多少。

The spooky typeof operator returns 'undefined' when its operand is a simple variable that does not exist, instead of throwing an error as would normally happen if you tried to refer to it. (You can also give it a simple variable wrapped in parentheses, but not a full expression involving a non-existant variable.) Not much use for that, either.

这是有争议的。当您访问不存在的对象的属性时,您不会立即收到类似于其他所有语言的错误。而是获得未定义的对象。 (然后当您尝试在脚本中稍后使用 undefined 对象时,它会以一种奇怪的方式出错,这比使用JavaScript时更难以追踪直接抛出错误。)

This is the controversial one. When you access a property of an object which doesn't exist, you don't immediately get an error like in every other language. Instead you get an undefined object. (And then when you try to use that undefined object later on in the script it'll go wrong in a weird way that's much more difficult to track down than if JavaScript had just thrown an error straight away.)

这通常用于检查属性是否存在:

This is often used to check for the existence of properties:

if (o.prop!==undefined) // or often as truthiness test, if (o.prop)
   ...do something...

但是,因为您可以像任何其他值一样分配 undefined

However, because you can assign undefined like any other value:

o.prop= undefined;

实际上并未检测到该属性是否可靠。最好在运算符中使用,该运算符不在原始的Netscape版本的JavaScript中,但现在随处可用:

that doesn't actually detect whether the property is there reliably. Better to use the in operator, which wasn't in the original Netscape version of JavaScript, but is available everywhere now:

if ('prop' in o)
    ...


总之, undefined 是一个特定于JavaScript的混乱,让每个人都感到困惑。除了可选的函数参数,JS没有其他更优雅的机制,应该避免使用 undefined 。它永远不应该成为语言的一部分; null 对于(2)和(3)来说就好了,而且(4)是一种只存在的错误,因为在开始时JavaScript没有例外。

In summary, undefined is a JavaScript-specific mess, which confuses everyone. Apart from optional function arguments, where JS has no other more elegant mechanism, undefined should be avoided. It should never have been part of the language; null would have worked just fine for (2) and (3), and (4) is a misfeature that only exists because in the beginning JavaScript had no exceptions.


if(!testvar)实际上是做什么的?它是测试undefined和null还是只是未定义?

what does if (!testvar) actually do? Does it test for undefined and null or just undefined?

这样的'真实性'测试检查 false undefined null 0 NaN 和空字符串。但在这种情况下,是的,它真的是 undefined 它关心的。 IMO,它应该更加明确,并说 if(testvar!== undefined)

Such a ‘truthiness’ test checks against false, undefined, null, 0, NaN and empty strings. But in this case, yes, it is really undefined it is concerned with. IMO, it should be more explicit about that and say if (testvar!==undefined).


一旦定义了变量,我可以将其清除为未定义(因此删除变量)。

once a variable is defined can I clear it back to undefined (therefore deleting the variable).

你当然可以为其分配 undefined ,但这不会删除变量。只有 delete object.property 运算符才能真正删除。

You can certainly assign undefined to it, but that won't delete the variable. Only the delete object.property operator really removes things.

delete 实际上是属于属性而不是变量。浏览器会让你直接使用删除变量,但这不是一个好主意,并且不适用于ECMAScript第五版的严格模式。如果你想释放对某事物的引用以便它可以被垃圾收集,那么更常见的是说 variable = null

delete is really meant for properties rather than variables as such. Browsers will let you get away with straight delete variable, but it's not a good idea and won't work in ECMAScript Fifth Edition's strict mode. If you want to free up a reference to something so it can be garbage-collected, it would be more usual to say variable= null.


我可以将undefined作为参数传递吗?

can I pass undefined as a parameter?

是。

这篇关于我可以将变量设置为undefined或将undefined作为参数传递吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆