JavaScript 为空

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

Javascript isnull

javascriptjqueryurl

提问by Phillip Senn

This is a really great function written in jQuery to determine the value of a url field:

这是一个用 jQuery 编写的非常棒的函数,用于确定 url 字段的值:

$.urlParam = function(name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    return results[1] || 0;
}

// example.com?someparam=name&otherparam=8&id=6
$.urlParam('someparam'); // name
$.urlParam('id'); // 6
$.urlParam('notavar'); // null

http://snipplr.com/view/11583/retrieve-url-params-with-jquery/

http://snipplr.com/view/11583/retrieve-url-params-with-jquery/

I would like to add a condition to test for null, but this looks kind of klunky:

我想添加一个条件来测试 null,但这看起来有点笨拙:

if (results == null) {
    return 0;
} else {
    return results[1] || 0;
}

Q: What's the elegant way to accomplish the above if/then statement?

问:完成上述 if/then 语句的优雅方式是什么?

回答by Brad

return results == null ? 0 : (results[1] || 0);

回答by Rich

return results == null ? 0 : ( results[1] || 0 );

回答by Dan Davies Brackett

the most terse solution would be to change return results[1] || 0;to return (results && results[1]) || 0.

最简洁的解决方案是更改return results[1] || 0;return (results && results[1]) || 0.

回答by streetparade

You could try this:

你可以试试这个:

if(typeof(results) == "undefined") { 
    return 0;
} else {
    return results[1] || 0;
}

回答by KingRider

Why not try .test()? ... Try its and best boolean (true or false):

为什么不试试.test()?... 尝试它和最好的布尔值(真或假):

$.urlParam = function(name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)');
    return results.test(window.location.href);
}

Tutorial:http://www.w3schools.com/jsref/jsref_regexp_test.asp

教程:http : //www.w3schools.com/jsref/jsref_regexp_test.asp

回答by Fatih

I'm using this function

我正在使用这个功能

function isNull() {
    for (var i = 0; i < arguments.length; i++) {
        if (
            typeof arguments[i] !== 'undefined'
            && arguments[i] !== undefined
            && arguments[i] != null
            && arguments[i] != NaN
            && arguments[i] 
        ) return arguments[i];
      }
}

test

测试

console.log(isNull(null, null, undefined, 'Target'));

回答by Teun D

return (results||0) && results[1] || 0;

The && operator acts as guard and returns the 0 if results if falsy and return the rightmost part if truthy.

&& 运算符充当守卫,如果结果为假则返回 0,如果为真则返回最右边的部分。

回答by Dan Ochiana

All mentioned solutions are legit but if we're talking about elegance then I'll pitch in with the following example:

所有提到的解决方案都是合法的,但如果我们谈论的是优雅,那么我将使用以下示例进行介绍:

//function that checks if an object is null
var isNull = function(obj) {
  return obj == null;
}            

if(isNull(results)){
 return 0;
} else {
  return results[1] || 0;
}

Using the isNull function helps the code be more readable.

使用 isNull 函数有助于代码更具可读性。

回答by Dan Ochiana

I prefer the style

我更喜欢这种风格

(results || [, 0]) [1]

回答by pixeline

if (typeof(results)!='undefined'){ 
    return results[1];
} else { 
    return 0; 
};

But you might want to check if results is an array. Arrays are of type Object so you will need this function

但是您可能想检查结果是否是一个数组。数组是 Object 类型,所以你需要这个函数

function typeOf(value) {
    var s = typeof value;
    if (s === 'object') {
        if (value) {
            if (value instanceof Array) {
                s = 'array';
            }
        } else {
            s = 'null';
        }
    }
    return s;
}

So your code becomes

所以你的代码变成

if (typeOf(results)==='array'){
      return results[1];
}
else
{
      return 0;
}