3

Possible Duplicate:
How to check a not defined variable in javascript
Determining if a javascript object has a given property

In my beforeSend function i have this

$.myloader.show();

But some scripts dont have that element so my this line gives me error

$.myloader.show(); gives me error that $.myloader does not exists

How can i do something like

if($.myloader exists)
$.myloader.show();
2
5

The most generic and solid solution is :

if (typeof $.myloader != 'undefined') {

If you're sure your variable can't hold anything else than a function or undefined, you might use

if ($.myloader) {

But do this only when you're sure of the possible values because this test also match false, 0 and ''.

5
  • It does not match '0', [], etc, and the test is reversed.
    – pimvdb
    Oct 11 '12 at 7:13
  • That's something different. if('0') will execute.
    – pimvdb
    Oct 11 '12 at 7:14
  • @pimvdb You're right, I got a little carried over... Oct 11 '12 at 7:15
  • 1
    No problem :). I still think the test is reversed, though.
    – pimvdb
    Oct 11 '12 at 7:26
  • @pimvdb I need a second coffee I think... I should not come on SO before coding... Oct 11 '12 at 7:32
0

You can do it as folllow...

if(typeof $.myloader != 'undefined')
{    // your code here.  }; 
2
  • You most certainly want !==, not !=
    – nfechner
    Oct 11 '12 at 7:12
  • @nfechner that doesn't seem needed : typeof always return a string. Oct 11 '12 at 7:18
0

Undefined values are 'falsey', and evaluate to false in an if statement, so all you need is:

if( $.myloader )
    $.myloader.show();
0

Given that jQuery ($) is an object:

if ($.hasOwnProperty('myloader'))
{
    //should work
    //try: $.hasOwnProperty('fn'), it'll return true, too
}

if the myloader object isn't directly bound to the jQuery object, but via one of its prototypes:

if ('myloader' in $)
{
    //works, like:
    if ('hasOwnProperty' in $)
    {//is true, but it's not a property/method of the object itself:
        console.log($.hasOwnProperty('hasOwnProperty'));//logs false
    }
}

You can check for the methods you want to use in the same way:

if ('myloader' in $ && 'show' in $.myloader)
{
    $.myloader.show();
}
2
  • $ is actually a function, but of course also inherits from Object... in the end, everything does (apart from Object.create(null)), so it should not really matter what $ is. Oct 11 '12 at 7:20
  • @FelixKling, yes, it's a function, but functions are first class objects. Tomato - tomato, really Oct 11 '12 at 7:23
0

You can do this even more shortly:

$.myloader.show() ? console.log('yep') : console.log('nope');

Or:

$.myloader.show() || console.log('fail');
0

If you want it short, you can also do it like this:

$.myloader && $.myloader.show();

The first operand (before the &&) is called a guard. However some people argue that this is not readable and safe enough, so you might want to use the if (typeof ...) solution.

Not the answer you're looking for? Browse other questions tagged or ask your own question.