简体   繁体   中英

What can I use instead of typeof in JS

If I have javascript array with objects inside, something like this

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.

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() ?

An alternative would be to just do this, which is quite typical in 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 . In that case, you would be better off checking if it is undefined using typeof .

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. There is no separate JavaScript equivalent to PHP's isset() .

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.

As everyone has said, you could just chain lots of typeof expressions. 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.

It may be more appropriate to do simply do this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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