代码之家  ›  专栏  ›  技术社区  ›  gautamlakum

在javascript/jquery中是否有类似于isset的php?[副本]

  •  71
  • gautamlakum  · 技术社区  · 15 年前

    这个问题已经有了答案:

    javascript/jquery中是否有检查变量是否已设置/可用的内容?在PHP中,我们使用 isset($variable) 像这样的检查。

    谢谢。

    11 回复  |  直到 9 年前
        1
  •  130
  •   Emil Vikström    12 年前

    尝试此表达式:

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

    如果变量是定义的而不是空的,这将是真的,这相当于php的isset的工作方式。

    您可以这样使用它:

    if(typeof(variable) != "undefined" && variable !== null) {
        bla();
    }
    
        2
  •  9
  •   Oleg    11 年前

    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;
    }
    
        3
  •  3
  •   DanneManne    15 年前

    我想这是我的目的

    if(typeof foo != "undefined"){}
    
        4
  •  3
  •   zzzzBov    15 年前

    如果要检查属性是否存在: hasOwnProperty 是去的路吗

    由于大多数对象都是其他对象的属性(最终导致 window 对象)这对于检查是否声明了值很有效。

        5
  •  2
  •   Ioannis Karadimas    15 年前

    不自然,不…然而,谷歌的东西给了这个: http://phpjs.org/functions/isset:454

        6
  •  2
  •   Sandeepan Nath    15 年前

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

    phpjs项目是受信任的源。这里有很多JS等价的PHP函数。我用了很长时间,到目前为止没有发现问题。

        7
  •  1
  •   Dieter Gribnitz    12 年前

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

    这意味着在将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:{};
    
        8
  •  0
  •   Mr Bill    12 年前

    这里:)

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

    除了 @埃米尔·维克斯特姆 的答案,正在检查 variable!=null 会是真的 variable!==null 以及 variable!==undefined (或) typeof(variable)!="undefined" )

        10
  •  0
  •   stuyam Marlow    10 年前

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

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

    下面是如何使用它的使用示例:

    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 检查变量是否为空(有些人显式设置为空,并且认为如果设置为空,则不正确,在这种情况下,请删除此部分)
    3. variable !== '' 检查变量是否设置为空字符串,如果空字符串计为用例的设置,则可以删除此变量。

    希望这能帮助别人:)

        11
  •  -1
  •   a1204773    12 年前

    您可以:

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