JQuery Object Discovery

I am writing a plugin for jQuery, and I want to make it so that the user can transfer data to the plugin in any form. I have a problem with JSON or an array, but I am having trouble trying to determine if the data is a jQuery object.

data = $('#list li');
console.debug( $.isPlainObject(data) );   // false
console.debug( $.isArray(data) );         // false
console.debug( data[0].tagName == "LI" ); // true, but see note below

The last method returns true, but there is no guarantee that the user will use the tag LIfor his data, so I think I need something like this:

if ( $.isjQueryObject(data) ) { /* do something */ }

Does anyone know a better method?

+3
source share
3 answers

jQuery ( $) , jQuery jQuery.prototype ( jQuery.fn).

, , instanceof, isPrototypeOf, :

function isjQueryObject(obj) {
  return obj instanceof jQuery;
}

:

function isjQueryObject(obj) {
  return jQuery.fn.isPrototypeOf(obj);
}
+9

jQuery , , . , jQuery , .

+1

What about:

var isJq = data instanceof jQuery;
+1
source

Source: https://habr.com/ru/post/1753017/


All Articles