If Not (oResponse.selectSingleNode("BigGroupType") Is Nothing) Then
End If
我需要将其转换为 javascript。这足以检查null吗?
这是我的领导者的答案,请验证这一点,
if(typeof $(data).find("BigGroupType").text() != "undefined" && $(data).find("BigGroupType").text() != null) {
}
If Not (oResponse.selectSingleNode("BigGroupType") Is Nothing) Then
End If
我需要将其转换为 javascript。这足以检查null吗?
这是我的领导者的答案,请验证这一点,
if(typeof $(data).find("BigGroupType").text() != "undefined" && $(data).find("BigGroupType").text() != null) {
}
JavaScript 有两个值,表示“无”,undefined和null. 比因为它是每个变量的默认值undefined具有更强的“无”含义。除非设置为,否则null没有变量可以是,但默认情况下是变量。nullnullundefined
var x;
console.log(x === undefined); // => true
var X = { foo: 'bar' };
console.log(X.baz); // => undefined
如果你想检查某个东西是否是undefined,你应该使用===because ==is not good 来区分它和null.
var x = null;
console.log(x == undefined); // => true
console.log(x === undefined); // => false
但是,这可能很有用,因为有时您想知道某物是否是undefined 或 null,因此您可以if (value == null)测试它是否是。
最后,如果你想测试一个变量是否存在于范围内,你可以使用typeof. 这在测试旧版浏览器中可能不存在的内置插件时会很有帮助,例如JSON.
if (typeof JSON == 'undefined') {
// Either no variable named JSON exists, or it exists and
// its value is undefined.
}
您需要同时检查nulland undefined,这隐含地这样做了
if( oResponse.selectSingleNode("BigGroupType") != null ) {
}
它相当于:
var node = oResponse.selectSingleNode("BigGroupType");
if( node !== null &&
node !== void 0 ) {
}
void 0作为一个防弹表达式得到undefined
这个逻辑:
If Not (oResponse.selectSingleNode("BigGroupType") Is Nothing)
在 JavaScript 中可以这样写:
if (typeof oResponse.selectSingleNode("BigGroupType") != 'undefined')
Nothing将等于undefined,但undefined出于多种原因不建议进行检查,通常使用 . 更安全typeof。
但是,如果selectSingleNode可以返回其他虚假值,例如null,则只需简单检查它是否为真就可以了:
if (oResponse.selectSingleNode("BigGroupType"))
在 JavaScript 中,Nothing 等价于undefined
if(oResponse.selectSingleNode("BigGroupType") != undefined){
}
JavaScript:-
(document.getElementById(“BigGroupType”) == undefined) // Returns true
jQuery:-
($(“#BigGroupType”).val() === “undefined”) // Returns true
注意在上面的例子中 undefined 是 JavaScript 中的一个关键字,而在 JQuery 中它只是一个字符串。