admin管理员组文章数量:1126967
Is there a fast way of checking if an object is a jQuery object or a native JavaScript object?
example:
var o = {};
var e = $('#element');
function doStuff(o) {
if (o.selector) {
console.log('object is jQuery');
}
}
doStuff(o);
doStuff(e);
obviously, the code above works but it's not safe. You could potentially add a selector key to the o
object and get the same result. Is there a better way of making sure that the object actually is a jQuery object?
Something in line with (typeof obj == 'jquery')
Is there a fast way of checking if an object is a jQuery object or a native JavaScript object?
example:
var o = {};
var e = $('#element');
function doStuff(o) {
if (o.selector) {
console.log('object is jQuery');
}
}
doStuff(o);
doStuff(e);
obviously, the code above works but it's not safe. You could potentially add a selector key to the o
object and get the same result. Is there a better way of making sure that the object actually is a jQuery object?
Something in line with (typeof obj == 'jquery')
9 Answers
Reset to default 955You can use the instanceof
operator:
if (obj instanceof jQuery){
console.log('object is jQuery');
}
Explanation: the jQuery
function (aka $
) is implemented as a constructor function. Constructor functions are to be called with the new
prefix.
When you call $(foo)
, internally jQuery translates this to new jQuery(foo)
1. JavaScript proceeds to initialize this
inside the constructor function to point to a new instance of jQuery
, setting it's properties to those found on jQuery.prototype
(aka jQuery.fn
). Thus, you get a new
object where instanceof jQuery
is true
.
1It's actually new jQuery.prototype.init(foo)
: the constructor logic has been offloaded to another constructor function called init
, but the concept is the same.
You may also use the .jquery property as described here: http://api.jquery.com/jquery-2/
var a = { what: "A regular JS object" },
b = $('body');
if ( a.jquery ) { // falsy, since it's undefined
alert(' a is a jQuery object! ');
}
if ( b.jquery ) { // truthy, since it's a string
alert(' b is a jQuery object! ');
}
Check out the instanceof operator.
var isJqueryObject = obj instanceof jQuery
The best way to check the instance of an object is through instanceof operator or with the method isPrototypeOf() which inspects if the prototype of an object is in another object's prototype chain.
obj instanceof jQuery;
jQuery.prototype.isPrototypeOf(obj);
But sometimes it might fail in the case of multiple jQuery instances on a document. As @Georgiy Ivankin mentioned:
if I have
$
in my current namespace pointing tojQuery2
and I have an object from outer namespace (where$
isjQuery1
) then I have no way to useinstanceof
for checking if that object is ajQuery
object
One way to overcome that problem is by aliasing the jQuery object in a closure or IIFE
//aliases jQuery as $
(function($, undefined) {
/*... your code */
console.log(obj instanceof $);
console.log($.prototype.isPrototypeOf(obj));
/*... your code */
}(jQuery1));
//imports jQuery1
Other way to overcome that problem is by inquiring the jquery
property in obj
'jquery' in obj
However, if you try to perform that checking with primitive values, it will throw an error, so you can modify the previous checking by ensuring obj
to be an Object
'jquery' in Object(obj)
Although the previous way is not the safest (you can create the 'jquery'
property in an object), we can improve the validation by working with both approaches:
if (obj instanceof jQuery || 'jquery' in Object(obj)) { }
The problem here is that any object can define a property jquery
as own, so a better approach would be to ask in the prototype, and ensure that the object is not null
or undefined
if (obj && (obj instanceof jQuery || obj.constructor.prototype.jquery)) { }
Due to coercion, the if
statement will make short circuit by evaluating the &&
operator when obj
is any of the falsy values (null
, undefined
, false
, 0
, ""
), and then proceeds to perform the other validations.
Finally we can write an utility function:
function isjQuery(obj) {
return (obj && (obj instanceof jQuery || obj.constructor.prototype.jquery));
}
Let's take a look at: Logical Operators and truthy / falsy
For those who want to know if an object is a jQuery object without having jQuery installed, the following snippet should do the work :
function isJQuery(obj) {
// All jQuery objects have an attribute that contains the jQuery version.
return typeof obj === "object" && obj != null && obj.jquery != null;
}
However, There is one more way to check the object in jQuery.
jQuery.type(a); //this returns type of variable.
I have made example to understand things, jsfiddle link
return el instanceof jQuery ? el.size() > 0 : (el && el.tagName);
You can check if the object is produced by JQuery with the jquery
property:
myObject.jquery // 3.3.1
=> return the number of the JQuery version if the object produced by JQuery.
=> otherwise, it returns undefined
var elArray = [];
var elObjeto = {};
elArray.constructor == Array //TRUE
elArray.constructor == Object//TALSE
elObjeto.constructor == Array//FALSE
elObjeto.constructor == Object//TRUE
本文标签: javascriptCheck if object is a jQuery objectStack Overflow
版权声明:本文标题:javascript - Check if object is a jQuery object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736689260a1947831.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
selector
property was deprecated long ago and removed in 3.0. Even in earlier versions, a jQuery object can have an empty selector string, for example$(window)
has no selector. Useinstanceof
instead. – Dave Methvin Commented Jul 18, 2016 at 15:43