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
  • Or stackoverflow.com/questions/858181/… (and other links visible at the right...) Commented Oct 11, 2012 at 7:17
  • try { some_var.xxx.xxx.xxx x = 't'; }catch(e){ x = 'f'; } console.debug(x); Commented Sep 11, 2015 at 7:56

6 Answers 6

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 ''.

Sign up to request clarification or add additional context in comments.

5 Comments

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

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.

Comments

0

You can do it as folllow...

if(typeof $.myloader != 'undefined')
{    // your code here.  }; 

2 Comments

You most certainly want !==, not !=
@nfechner that doesn't seem needed : typeof always return a string.
0

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

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

Comments

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 Comments

$ 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.
@FelixKling, yes, it's a function, but functions are first class objects. Tomato - tomato, really
0

You can do this even more shortly:

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

Or:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.