How to programmatically determine if an object is a jQuery object?

How can I programmatically determine if an object is a jQuery object? For instance:

// 1. declare some variables
var vars = [1, 'hello', ['bye', 3], $(body), { say: 'hi' }];

// 2. ??? : implement function that tests whether its parameter is a jQuery object
function isjQuery(arg) { /* ??? */ }

// 3. profit
var test = $.map(vars, isjQuery); /* expected [ false, false, false, true, false ] */
+5
source share
3 answers

I think you can rely on

if ( vars[i] instanceof jQuery ) {
  // do something with this jQuery object
}

but I also found these methods here :

obj && obj.constructor == jQuery 
obj && obj.jquery 
+4
source

The easiest API-documented way is to check the property .jquery:

function isjQuery(arg) {
    return !!arg.jquery;
}

However, if you want to be sure , this is a jQuery object, and not some other object with a fake property .jquery, other answers suggesting instanceof jQueryand testing the work of the constructor too.

(.jquery , jQuery, API , jQuery.)

+9

, ( ) :

function isjQuery(arg) { return arg instanceof jQuery; }
+4
source

All Articles