Javascript 测试(对象 && 对象 !== "null" && object !== "undefined" )

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12535403/
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-08-23 10:39:12  来源:igfitidea点击:

Javascript test ( object && object !== "null" && object !== "undefined" )

javascript

提问by Nick G.

I seem to be using this test a lot

我似乎经常使用这个测试

if( object && object !== "null" && object !== "undefined" ){
    doSomething();
}

on objectsI get back from a service call or from reading cookies (since different browsers return the different values null, undefined, "null", or "undefined").

objects我从一个服务调用或读取饼干回来(因为不同的浏览器返回不同的值nullundefined"null",或"undefined")。

Is there an easier/more efficient way of doing this check?

有没有更简单/更有效的方法来做这个检查?

回答by aquinas

I don't think you can make that any simpler, but you could certainly refactor that logic into a function:

我认为你不能让它变得更简单,但你当然可以将该逻辑重构为一个函数:

function isRealValue(obj)
{
 return obj && obj !== 'null' && obj !== 'undefined';
}

Then, at least your code becomes:

然后,至少您的代码变为:

if (isRealValue(yourObject))
{
 doSomething();
}

回答by tuchi35

If you have jQuery, you could use $.isEmptyObject().

如果你有 jQuery,你可以使用$.isEmptyObject().

$.isEmptyObject(null)
$.isEmptyObject(undefined)
var obj = {}
$.isEmptyObject(obj)

All these calls will return true. Hope it helps

所有这些调用都将返回 true。希望能帮助到你

回答by Sean Bradley

if(!!object){
  doSomething();
}

回答by scatter

If objectis truthy, we already know that it is not nullor undefined(assuming that the string values are a mistake). I assume that a not nulland not undefinedtest is wanted.

如果object是真的,我们已经知道它不是nullundefined(假设字符串值是错误的)。我假设需要一个 notnull和 notundefined测试。

If so, a simple comparison to nullor undefinedis to compare != null.

如果是这样,与null或的简单比较undefined就是比较!= null

if( object != null ){
    doSomething();
}

The doSomethingfunction will run only if objectis neither nullnor undefined.

doSomething仅当object既不是null也不是 时,该函数才会运行undefined

回答by Snake Eyes

maybe like this

也许像这样

   if (typeof object !== "undefined" || object !== null)
    // do something

回答by AndriyFM

Maybe like this:

也许是这样的:

var myObj = {};
var isEmptyObj = !Object.keys(myObj).length;

if(isEmptyObj) {
    // true
} else {

回答by Harish Mahajan

The best way to check if an object is empty is by using a utility function like the one below.

检查对象是否为空的最佳方法是使用如下所示的实用程序函数。

create a function

创建一个函数

function isEmpty(obj) {
    for(var key in obj) {
        if(obj.hasOwnProperty(key))
            return false;
    }
    return true;
}

Use above function following way:-

按照以下方式使用上述功能:-

So if you have an empty object, you can check whether it is empty by using the above function.

所以如果你有一个空对象,你可以使用上面的函数检查它是否为空。

var myObj = {}; // Empty Object
if(isEmpty(myObj)) {
    // Object is empty (Would return true in this example)
} else {
    // Object is NOT empty
}

回答by lmiguelvargasf

I think you could simplify a bit your logic with the following:

我认为您可以通过以下方式简化您的逻辑:

if (object != null && typeof(object) == "object") {
    doSomething();
}

The main problem is that if you just check typeof(object) == "object", it will return trueif objectis nullsince null's type is "object". However, if you first check that object != null, you can be sure you are having something that is neither undefinednor null.

主要的问题是,如果你只是检查typeof(object) == "object",它会返回trueif objectisnull因为它null的类型是"object"。但是,如果您首先检查object != null,您可以确定您所拥有的东西既不是 也不undefinednull

回答by Sampath

This should work without any issue.

这应该可以正常工作。

if(object){ // checks for null and undefined
    doSomething();
}