I'm a bit confused about Javascript undefined & null.
(我对Javascript undefined&null感到有点困惑。)
Don't be confused about null
.
(不要对null
感到困惑。)
It generally makes sense and behaves similarly to other scripting languages' concepts of the out-of-band 'null', 'nil' or 'None' objects.(它通常是有意义的,并且与其他脚本语言的带外“null”,“nil”或“None”对象的概念类似。)
undefined
, on the other hand, is a weird JavaScript quirk.
(另一方面, undefined
是一个奇怪的JavaScript怪癖。)
It's a singleton object that represents out-of-band values, essentially a second similar-but-different null
.(它是一个表示带外值的单例对象,实质上是第二个相似但不同的null
。)
It comes up:(它出现了:)
When you call a function with fewer arguments than the arguments list in the function
statement lists, the unpassed arguments are set to undefined
.
(当您调用参数少于function
语句列表中的参数列表的function
,未批处理的参数将设置为undefined
。)
You can test for that with eg.:(您可以使用例如:)
function dosomething(arg1, arg2) { if (arg2===undefined) arg2= DEFAULT_VALUE_FOR_ARG2; ... }
With this method you can't tell the difference between dosomething(1)
and dosomething(1, undefined)
;
(使用这种方法你无法分辨dosomething(1)
和dosomething(1, undefined)
之间的区别;)
arg2
will be the same value in both.(arg2
在两者中都是相同的值。)
If you need to tell the difference you can look at arguments.length
, but doing optional arguments like that isn't generally very readable.(如果你需要区分你可以看看arguments.length
,但是做那样的可选参数通常不是很易读。)
When a function has no return value;
(当函数没有return value;
)
, it returns undefined
.(,它返回undefined
。)
There's generally no need to use such a return result.(通常不需要使用这样的返回结果。)
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
.
(通过在块中包含var a
语句来声明变量但尚未为其赋值时,它是undefined
。)
Again, you shouldn't really ever need to rely on that.(再说一遍,你真的不需要依赖它。)
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.
(当spooky typeof
运算符的操作数是一个不存在的简单变量时,它会返回'undefined'
,而不是像你试图引用它时那样抛出错误。)
(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.((你也可以给它一个包含在括号中的简单变量,但不是一个涉及不存在的变量的完整表达式。)也没用多少。)
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.(相反,你得到一个undefined
对象。)
(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.)((然后当你尝试在脚本中使用那个undefined
对象时,它会以一种奇怪的方式出错,这种方式比JavaScript直接抛出错误要困难得多。))
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...
However, because you can assign undefined
like any other value:
(但是,因为您可以像任何其他值一样分配undefined
:)
o.prop= undefined;
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:(最好使用in
运算符,它不是原始的Netscape版本的JavaScript,但现在随处可用:)
if ('prop' in o) ...
In summary, undefined
is a JavaScript-specific mess, which confuses everyone.
(总之, undefined
是一个特定于JavaScript的混乱,让每个人都感到困惑。)
Apart from optional function arguments, where JS has no other more elegant mechanism, undefined
should be avoided.(除了可选的函数参数,JS没有其他更优雅的机制,应该避免使用undefined
。)
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.(null
对于(2)和(3)来说效果很好,而(4)只是存在的错误,因为在开始时JavaScript没有例外。)
what does if (!testvar)
actually do?
(if (!testvar)
实际上做了什么?)
Does it test for undefined and null or just undefined?(它是测试undefined和null还是只是未定义?)
Such a 'truthiness' test checks against false
, undefined
, null
, 0
, NaN
and empty strings.
(这种“真实性”测试检查false
, undefined
, null
, 0
, NaN
和空字符串。)
But in this case, yes, it is really undefined
it is concerned with.(但在这种情况下,是的,它确实是undefined
。)
IMO, it should be more explicit about that and say if (testvar!==undefined)
.(IMO,它应该更明确,并说if (testvar!==undefined)
。)
once a variable is defined can I clear it back to undefined (therefore deleting the variable).
(一旦定义了变量,我可以将其清除为未定义(因此删除变量)。)
You can certainly assign undefined
to it, but that won't delete the variable.
(你当然可以为它分配undefined
,但这不会删除变量。)
Only the delete object.property
operator really removes things.(只有delete object.property
运算符才真正删除了东西。)
delete
is really meant for properties rather than variables as such.
(delete
实际上是指属性而不是变量。)
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.(浏览器会让你逃脱直接delete variable
,但这不是一个好主意,不会在ECMAScript第五版的严格模式下工作。)
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
.(如果你想释放对某事物的引用以便它可以被垃圾收集,那么更常见的是variable= null
。)
can I pass undefined as a parameter?
(我可以将undefined作为参数传递吗?)
Yes.
(是。)