javascript/jQuery 中有类似 php isset 的东西吗?

发布于 2024-10-03 10:57:17 字数 96 浏览 4 评论 0原文

javascript/jQuery 中有没有东西可以检查变量是否设置/可用?在 php 中,我们使用 isset($variable) 来检查类似的内容。

谢谢。

Is there something in javascript/jQuery to check whether variable is set/available or not? In php, we use isset($variable) to check something like this.

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

追我者格杀勿论 2024-10-10 10:57:17

尝试这个表达式:

typeof(variable) != "undefined" && variable !== null

如果变量已定义且不为 null,则该表达式为 true,这相当于 PHP 的 isset 的工作方式。

你可以这样使用它:

if(typeof(variable) != "undefined" && variable !== null) {
    bla();
}

Try this expression:

typeof(variable) != "undefined" && variable !== null

This will be true if the variable is defined and not null, which is the equivalent of how PHP's isset works.

You can use it like this:

if(typeof(variable) != "undefined" && variable !== null) {
    bla();
}
南风几经秋 2024-10-10 10:57:17

PHP JS 上的 JavaScript isset()

function isset () {
    // discuss at: http://phpjs.org/functions/isset
    // +   original by: Kevin van     Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: FremyCompany
    // +   improved by: Onno Marsman
    // +   improved by: Rafał Kukawski
    // *     example 1: isset( undefined, true);
    // *     returns 1: false
    // *     example 2: isset( 'Kevin van Zonneveld' );
    // *     returns 2: true
    var a = arguments,
        l = a.length,
        i = 0,
        undef;

    if (l === 0) {
        throw new Error('Empty isset');
    }

    while (i !== l) {
        if (a[i] === undef || a[i] === null) {
            return false;
        }
        i++;
    }
    return true;
}

JavaScript isset() on PHP JS

function isset () {
    // discuss at: http://phpjs.org/functions/isset
    // +   original by: Kevin van     Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: FremyCompany
    // +   improved by: Onno Marsman
    // +   improved by: Rafał Kukawski
    // *     example 1: isset( undefined, true);
    // *     returns 1: false
    // *     example 2: isset( 'Kevin van Zonneveld' );
    // *     returns 2: true
    var a = arguments,
        l = a.length,
        i = 0,
        undef;

    if (l === 0) {
        throw new Error('Empty isset');
    }

    while (i !== l) {
        if (a[i] === undef || a[i] === null) {
            return false;
        }
        i++;
    }
    return true;
}
紫瑟鸿黎 2024-10-10 10:57:17

typeof 会达到我认为的目的

if(typeof foo != "undefined"){}

typeof will serve the purpose I think

if(typeof foo != "undefined"){}
在梵高的星空下 2024-10-10 10:57:17

如果您想检查属性是否存在: hasOwnProperty 是 由于大多数

对象都是其他对象的属性(最终导致 window 对象),因此这可以很好地检查值是否已声明。

If you want to check if a property exists: hasOwnProperty is the way to go

And since most objects are properties of some other object (eventually leading to the window object) this can work well for checking if values have been declared.

日暮斜阳 2024-10-10 10:57:17

每个答案的某些部分都有效。我将它们全部编译成一个函数“isset”,就像问题所问的那样,并且像在 PHP 中一样工作。

// isset 辅助函数 
var isset = 函数(变量){
    return typeof(variable) !== "undefined" &&变量 !== null &&变量!=='';
}

以下是如何使用它的用法示例:

var example = 'this is an example';
if(isset(example)){
    console.log('the example variable has a value set');
}

这取决于您需要它的情况,但让我分解一下每个部分的作用:

  1. typeof(variable) !== "undefined" 检查是否变量完全被定义
  2. variable !== null 检查变量是否为 null(有些人显式设置 null 并且不认为设置为 null 就是正确的,在这种情况下,删除这部分)
  3. variable !== '' 检查变量是否设置为空字符串,如果空字符串被视为为您的用例设置,您可以删除它

希望这对某人有帮助:)

Some parts of each of these answers work. I compiled them all down into a function "isset" just like the question was asking and works like it does in PHP.

// isset helper function 
var isset = function(variable){
    return typeof(variable) !== "undefined" && variable !== null && variable !== '';
}

Here is a usage example of how to use it:

var example = 'this is an example';
if(isset(example)){
    console.log('the example variable has a value set');
}

It depends on the situation you need it for but let me break down what each part does:

  1. typeof(variable) !== "undefined" checks if the variable is defined at all
  2. variable !== null checks if the variable is null (some people explicitly set null and don't think if it is set to null that that is correct, in that case, remove this part)
  3. variable !== '' checks if the variable is set to an empty string, you can remove this if an empty string counts as set for your use case

Hope this helps someone :)

夏末染殇 2024-10-10 10:57:17

不自然,不...但是,谷歌搜索给出了这个: http://phpjs.org/功能/设置:454

Not naturally, no... However, a googling of the thing gave this: http://phpjs.org/functions/isset:454

时光病人 2024-10-10 10:57:17

http://phpjs.org/functions/isset:454

phpjs 项目是值得信赖的来源。那里有很多 js 等效的 php 函数。我已经使用了很长时间了,到目前为止没有发现任何问题。

http://phpjs.org/functions/isset:454

phpjs project is a trusted source. Lots of js equivalent php functions available there. I have been using since a long time and found no issues so far.

策马西风 2024-10-10 10:57:17

问题在于将未定义的变量传递给函数会导致错误。

这意味着您必须在将其作为参数传递之前运行 typeof 。

我发现执行此操作的最干净的方法如下所示:

function isset(v){
    if(v === 'undefined'){
        return false;
    }
    return true;
}

用法:

if(isset(typeof(varname))){
  alert('is set');
} else {
  alert('not set');
}

现在代码更加紧凑和可读。

如果您尝试从非实例化变量调用变量,这仍然会给出错误:

isset(typeof(undefVar.subkey))

因此在尝试运行此变量之前,您需要确保定义了该对象:

undefVar = isset(typeof(undefVar))?undefVar:{};

The problem is that passing an undefined variable to a function causes an error.

This means you have to run typeof before passing it as an argument.

The cleanest way I found to do this is like so:

function isset(v){
    if(v === 'undefined'){
        return false;
    }
    return true;
}

Usage:

if(isset(typeof(varname))){
  alert('is set');
} else {
  alert('not set');
}

Now the code is much more compact and readable.

This will still give an error if you try to call a variable from a non instantiated variable like:

isset(typeof(undefVar.subkey))

thus before trying to run this you need to make sure the object is defined:

undefVar = isset(typeof(undefVar))?undefVar:{};
请止步禁区 2024-10-10 10:57:17

这里 :)

function isSet(iVal){
 return (iVal!=="" && iVal!=null && iVal!==undefined && typeof(iVal) != "undefined") ? 1 : 0;
} // Returns 1 if set, 0 false

Here :)

function isSet(iVal){
 return (iVal!=="" && iVal!=null && iVal!==undefined && typeof(iVal) != "undefined") ? 1 : 0;
} // Returns 1 if set, 0 false
年少掌心 2024-10-10 10:57:17

除了 @emil-vikström 的答案之外,检查 variable!=null 对于 variable!==null 以及对于变量!==未定义(或typeof(变量)!=“未定义”)。

in addition to @emil-vikström's answer, checking for variable!=null would be true for variable!==null as well as for variable!==undefined (or typeof(variable)!="undefined").

爱格式化 2024-10-10 10:57:17

你可以:

if(variable||variable===0){
    //Yes it is set
    //do something
}
else {
    //No it is not set
    //Or its null
    //do something else 
}

You can just:

if(variable||variable===0){
    //Yes it is set
    //do something
}
else {
    //No it is not set
    //Or its null
    //do something else 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文