代码之家  ›  专栏  ›  技术社区  ›  David Hellsing

检查对象是否为jQuery对象

  •  561
  • David Hellsing  · 技术社区  · 16 年前

    例子:

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

    9 回复  |  直到 12 年前
        1
  •  908
  •   Joe DF    5 年前

    你可以使用 instanceof 接线员:

    if (obj instanceof jQuery){
        console.log('object is jQuery');
    }
    

    解释 jQuery 功能(又名 $ )作为一个 constructor function . 构造函数将使用 new 前缀

    $(foo) ,jQuery内部将其转换为 new jQuery(foo) 1. this 在构造函数中指向的新实例 ,将其属性设置为在上找到的属性 jQuery.prototype (又名 jQuery.fn 物体在哪里 instanceof jQuery true .


    1. 实际上是 new jQuery.prototype.init(foo) init ,但概念是一样的。

        2
  •  111
  •   mt81    15 年前

    您还可以使用.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! ');
    }
    
        3
  •  32
  •   Corey Sunwold    16 年前

    查看 instanceof 操作人员

    var isJqueryObject = obj instanceof jQuery
    
        4
  •  27
  •   jherax    10 年前

    instanceof 还是用这个方法 isPrototypeOf() 它检查一个对象的原型是否在另一个对象的原型链中。

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

    但有时,如果一个文档上有多个jQuery实例,它可能会失败。正如@Georgiy Ivankin提到的:

    $ 在我当前的命名空间中指向 jQuery2 是 jQuery1 )那我就没办法用了 instanceof jQuery 对象

    克服这个问题的一种方法是在 closure IIFE

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

    克服这个问题的另一种方法是询问 jquery 财产 obj

    'jquery' in obj
    

    obj Object

    'jquery' in Object(obj)
    

    'jquery' 属性),我们可以通过使用这两种方法来改进验证:

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

    这里的问题是任何对象都可以定义属性 jquery null 或 undefined

    if (obj && (obj instanceof jQuery || obj.constructor.prototype.jquery)) { }
    

    由于 胁迫 这个 if 语句将通过评估 && 接线员什么时候 有什么问题吗 法尔西 , , false , 0 , ""

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

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

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

        5
  •  4
  •   Shaunak Shukla    9 年前

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

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

    我做了榜样来理解事情, jsfiddle link

        6
  •  3
  •   j0k gauthamp    14 年前
    return el instanceof jQuery ? el.size() > 0 : (el && el.tagName);
    
        7
  •  2
  •   Karl.S    8 年前

    对于那些想知道对象是否是未安装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"];
    }
    
        8
  •  2
  •   Jérôme-Victor Toulouse    7 年前

    您可以使用 jquery

    myObject.jquery // 3.3.1
    

    =>否则,它将返回 undefined

        9
  •  -9
  •   Gabriel Seg    11 年前
    var elArray = [];
    var elObjeto = {};
    
    elArray.constructor == Array //TRUE
    elArray.constructor == Object//TALSE
    
    elObjeto.constructor == Array//FALSE
    elObjeto.constructor == Object//TRUE