简体   繁体   English

我可以用什么代替JS中的typeof

[英]What can I use instead of typeof in JS

If I have javascript array with objects inside, something like this 如果我的JavaScript数组中有对象,则类似这样

articlesParams[itemId].properties.loaded

If I try do this: 如果我尝试这样做:

if ( typeof articlesParams[itemId].properties.loaded != 'undefined' && ...) {
    // do something...
}

I have encountered with problem that if articleParams[itemId].properties is undefined I got an error: Cannot read property 'properties' of undefined. 我遇到的问题是,如果未定义articleParams [itemId] .properties,则会收到错误消息:无法读取未定义的属性'properties'。

So in this case I should do something stupid like this: 所以在这种情况下,我应该做一些愚蠢的事情:

if ( typeof articlesParams[itemId].properties != 'undefined' &&  
        typeof articlesParams[itemId].properties.loaded != 'undefined'  && ...) {
    // do something...
}

Is any equivalent in javascript like in PHP isset() ? 在JavaScript中是否有与PHP isset()类似的等效项?

An alternative would be to just do this, which is quite typical in JS: 一种替代方法是仅执行此操作,这在JS中非常典型:

if (articlesParams[itemId] &&
    articlesParams[itemId].properties &&  
    articlesParams[itemId].properties.loaded  && ...) {
    // do something...
}

or this: 或这个:

var item = articlesParams[itemId];
if (item && item.properties && item.properties.loaded  && ...) {
    // do something...
}

One point to note is that this will not work if you want the if statement to execute even if loaded is false . 需要注意的一点是,即使要让if语句执行,即使loadedfalse ,也无法使用。 In that case, you would be better off checking if it is undefined using typeof . 在这种情况下,最好使用typeof检查它是否undefined

Although verbose, the standard JavaScript idiom for checking whether a variable is undefined does use typeof and in fact looks much like what you wrote. 尽管很冗长, 但用于检查变量是否未定义的标准JavaScript惯用法确实使用typeof ,实际上看起来很像您编写的内容。 There is no separate JavaScript equivalent to PHP's isset() . 没有与PHP的isset()等效的单独JavaScript。

Only two minor improvements to offer: 仅提供两个小改进:

Like this: 像这样:

var item = articlesParams[itemId];
if (typeof item !== 'undefined') {
   var props = item.properties;
   if (typeof props !== 'undefined') {
       var loaded = props.loaded;
       if (typeof loaded !== 'undefined') {
           // do something...
       }
   }
}

Of course, if loaded , props , etc aren't re-used much, there's little motivation for defining vars for them. 当然,如果未重用loadedprops等,则没有动力为它们定义var。

As everyone has said, you could just chain lots of typeof expressions. 正如每个人都说过的那样,您可以将许多typeof表达式链接在一起。 Alternatively.... 另外....

var result;
try {
   result=typeof articlesParams[itemId].properties.loaded;
} catch(e) {
   result='undefined';
}
if ('undefined'==result) {
    // do something
}

But there's a risk that you might apply this to a method rather than a variable and hence it should throw a different kind of exception. 但是,存在将其应用于方法而不是变量的风险,因此它应该引发另一种异常。 Further, it can't simply be wrapped up as a function (since the exception will occur when you try to call the function, and not in the function body). 此外,它不能简单地包装为一个函数(因为在您尝试调用该函数时会发生异常,而不是在函数体中)。 And there's little savings in the amount of typing you need to do or the ease of understanding what the code does compared to chained typeof expressions. 与链接的typeof表达式相比,您需要做的打字量或易于理解代码功能的节省很少。

It may be more appropriate to do simply do this: 简单地执行以下操作可能更合适:

try {
   typeof articlesParams[itemId].properties.loaded;
} catch (e) {
   // do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM