vbscript `Is Nothing` 的 javascript 等效项是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13763495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:43:07  来源:igfitidea点击:

What is the javascript equivalant for vbscript `Is Nothing`

javascriptvbscript

提问by Madura Harshana

If Not (oResponse.selectSingleNode("BigGroupType") Is Nothing) Then

End If

I need to convert this to javascript. Is that enough to check null?

我需要将其转换为 javascript。足以检查null吗?

This was my lead's answer, plz verify this,

这是我的领导的回答,请验证这一点,

if(typeof $(data).find("BigGroupType").text() !=  "undefined" && $(data).find("BigGroupType").text() != null) {  

}

回答by Nathan Wall

JavaScript has two values which mean "nothing", undefinedand null. undefinedhas a much stronger "nothing" meaning than nullbecause it is the default value of every variable. No variable can be nullunless it is setto null, but variables are undefinedby default.

JavaScript 有两个值,表示“无”,undefinednull. undefinednull因为它是每个变量的默认值具有更强的“无”含义。null除非设置null,否则没有变量可以是,但undefined默认情况下变量是。

var x;
console.log(x === undefined); // => true

var X = { foo: 'bar' };
console.log(X.baz); // => undefined

If you want to check to see if something is undefined, you should use ===because ==isn't good enough to distinguish it from null.

如果您想检查某物是否为undefined,则应使用===因为==不足以将其与null.

var x = null;
console.log(x == undefined); // => true
console.log(x === undefined); // => false

However, this can be useful because sometimes you want to know if something is undefinedornull, so you can just do if (value == null)to test if it is either.

但是,这可能很有用,因为有时您想知道某物是否为undefinednull,因此您可以if (value == null)测试它是否为 或 。

Finally, if you want to test whether a variable even exists in scope, you can use typeof. This can be helpful when testing for built-ins which may not exist in older browsers, such as JSON.

最后,如果你想测试一个变量是否存在于作用域中,你可以使用typeof. 这在测试旧浏览器中可能不存在的内置程序时会很有帮助,例如JSON.

if (typeof JSON == 'undefined') {
    // Either no variable named JSON exists, or it exists and
    // its value is undefined.
}

回答by Esailija

You need to check for both nulland undefined, this implicitly does so

您需要检查nullundefined,这隐含地这样做

if( oResponse.selectSingleNode("BigGroupType") != null ) {

}

It is the equivalent of:

它相当于:

var node = oResponse.selectSingleNode("BigGroupType");
if( node !== null &&
    node !== void 0 ) {

}

void 0being a bulletproof expression to get undefined

void 0作为一个防弹表达式来获得 undefined

回答by David Hellsing

This logic:

这个逻辑:

If Not (oResponse.selectSingleNode("BigGroupType") Is Nothing)

Can be written like this in JavaScript:

在 JavaScript 中可以这样写:

if (typeof oResponse.selectSingleNode("BigGroupType") != 'undefined')

Nothingwould equal undefined, but checking against undefinedis not recommended for several reasons, it's generally safer to use typeof.

Nothing将等于undefined,但undefined出于多种原因不建议检查,使用typeof.

However, if the selectSingleNodecan return other falsy values such as null, it's probably OK to just do a simple check if it is truthy:

但是,如果selectSingleNode可以返回其他虚假值,例如null,则只需简单检查它是否为真值就可以了:

if (oResponse.selectSingleNode("BigGroupType"))

回答by Epsil0neR

In JavaScript equvalent for Nothing is undefined

在 JavaScript 中,Nothing 等价于 undefined

if(oResponse.selectSingleNode("BigGroupType") != undefined){

}

回答by Madura Harshana

JavaScript:-

JavaScript:-

(document.getElementById(“BigGroupType”) == undefined) // Returns true

(document.getElementById(“BigGroupType”) == undefined) // Returns true

JQuery:-

jQuery:-

($(“#BigGroupType”).val() === “undefined”) // Returns true

($(“#BigGroupType”).val() === “undefined”) // Returns true

Note in above examples undefined is a keyword in JavaScript, where as in JQuery it is just a string.

注意在上面的例子中 undefined 在 JavaScript 中是一个关键字,而在 JQuery 中它只是一个字符串。