首页 文章

检查对象是否是jQuery对象

提问于
浏览
552

有没有一种快速检查对象是jQuery对象还是本机JavaScript对象的方法?

例:

var o = {};
var e = $('#element');

function doStuff(o) {
    if (o.selector) {
        console.log('object is jQuery');
    }
}

doStuff(o);
doStuff(e);

显然,上面的代码有效,但不安全 . 您可以将选择器键添加到 o 对象并获得相同的结果 . 有没有更好的方法来确保对象实际上是一个jQuery对象?

符合 (typeof obj == 'jquery') 的东西

8 回答

  • 22

    您可以使用instanceof运算符:

    obj instanceof jQuery
    

    ExplanationjQuery 函数(又名 $ )实现为constructor function . 使用 new 前缀调用构造函数 .

    当您调用 $(foo) 时,内部jQuery将其转换为 new jQuery(foo) 1. JavaScript继续在构造函数内初始化 this 以指向 jQuery 的新实例,将其属性设置为 jQuery.prototype (又名 jQuery.fn )上的属性 . 因此,您得到一个 new 对象,其中 instanceof jQuerytrue .


    1它实际上是新的jQuery.prototype.init(foo):构造函数逻辑已被卸载到另一个名为init的构造函数,但概念是相同的 .

  • 3

    您也可以使用此处所述的.jquery属性: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! ');
    }
    
  • 2

    查看instanceof运算符 .

    var isJqueryObject = obj instanceof jQuery
    
  • -10

    检查对象实例的最佳方法是通过 instanceof operator 或方法isPrototypeOf(),它检查对象的原型是否在另一个对象的原型链中 .

    obj instanceof jQuery;
    jQuery.prototype.isPrototypeOf(obj);
    

    但有时在文档上有多个jQuery实例的情况下可能会失败 . 正如@Georgiy Ivankin所说:

    如果我当前的命名空间中有$指向jQuery2并且我有一个来自外部命名空间的对象(其中$是jQuery1),那么我无法使用instanceof来检查该对象是否是一个jQuery对象

    解决该问题的一种方法是在 closureIIFE 中对jQuery对象进行别名处理

    //aliases jQuery as $
    (function($, undefined) {
        /*... your code */
    
        console.log(obj instanceof $);
        console.log($.prototype.isPrototypeOf(obj));
    
        /*... your code */
    }(jQuery1));
    //imports jQuery1
    

    克服该问题的其他方法是通过查询 obj 中的 jquery 属性

    'jquery' in obj
    

    但是,如果您尝试使用原始值执行该检查,则会抛出错误,因此您可以通过确保 objObject 来修改先前的检查

    'jquery' in Object(obj)
    

    虽然前面的方法不是最安全的(您可以在对象中创建 'jquery' 属性),但我们可以通过使用这两种方法来改进验证:

    if (obj instanceof jQuery || 'jquery' in Object(obj)) { }
    

    这里的问题是任何对象都可以将属性 jquery 定义为自己的属性,因此更好的方法是在原型中询问,并确保该对象不是 nullundefined ****
    if (obj && (obj instanceof jQuery || obj.constructor.prototype.jquery)) { }
    **** 由于 coercion ,当 objfalsy 值( nullundefinedfalse0"" )中的任何一个时, if 语句将通过评估 && 运算符进行短路,然后继续执行其他验证 .

    最后我们可以编写一个实用函数:

    function isjQuery(obj) {
      return (obj && (obj instanceof jQuery || obj.constructor.prototype.jquery));
    }
    

    我们来看看:Logical Operators and truthy / falsy

  • 103
    return el instanceof jQuery ? el.size() > 0 : (el && el.tagName);
    
  • 818

    但是,还有一种方法可以在jQuery中检查对象 .

    jQuery.type(a); //this returns type of variable.
    

    我已经做了一些了解事情的例子,jsfiddle link

  • 3

    对于那些想要在没有安装jQuery的情况下想知道某个对象是否是jQuery对象的人,以下代码片段应该可以完成这项工作:

    function isJQuery(obj) {
      // Each jquery object has a "jquery" attribute that contains the version of the lib.
      return typeof obj === "object" && obj && obj["jquery"];
    }
    
  • 30
    var elArray = [];
    var elObjeto = {};
    
    elArray.constructor == Array //TRUE
    elArray.constructor == Object//TALSE
    
    elObjeto.constructor == Array//FALSE
    elObjeto.constructor == Object//TRUE
    

相关问题