什么时候在JavaScript中使用null或undefined? [英] When is null or undefined used in JavaScript?

查看:184
本文介绍了什么时候在JavaScript中使用null或undefined?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很困惑JavaScript何时返回 null undefined 。另外,不同的浏览器似乎也以不同的方式返回。

I am really confused as to when JavaScript returns null or undefined. Also different browsers seem to be returning these differently.

请举例说明 null / undefined 使用返回它们的浏览器。

Could you please give some examples of null/undefined with the browsers that return them.

我现在已经清楚了 undefined 方面,我仍然没有100%明确 null 。它是否类似于空白值?

While I am now clear on the undefined aspect, I am still not 100% clear on null. Is it similar to a blank value?

例如。您有一个没有任何值设置的文本框。现在,当您尝试访问其值时,它是 null 还是 undefined 并且它们是否相似?

E.g. You have a text box which does not have any value set. Now when you try to access its value, will it be null or undefined and are they similar?

推荐答案

DOM方法 getElementById() nextSibling() childNodes [n] parentNode()等等返回<$ c $当调用未返回节点对象时,c> null (已定义但没有值)。

The DOM methods getElementById(), nextSibling(), childNodes[n], parentNode() and so on return null (defined but having no value) when the call does not return a node object.

属性已定义,但它所引用的对象不存在。

The property is defined, but the object it refers to does not exist.

这是您想要测试相等性的少数几次之一 -

This is one of the few times you may not want to test for equality-

if(x! == undefined)对于空值为真

但是 if(x!= undefined) undefined null 的值,c $ c>将为true(仅)。

but if(x!= undefined) will be true (only) for values that are not either undefined or null.

这篇关于什么时候在JavaScript中使用null或undefined?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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