Why is it possible to query jQuery('div') like an array?

后端 未结 5 1319
礼貌的吻别
礼貌的吻别 2021-01-05 01:59

I got another question regarding jQuery\'s architecture. $(\'div\') constructs a new jQuery object:

$(\'div\') instanceof jQuery; /         


        
5条回答
  •  北海茫月
    2021-01-05 02:58

    jQuery objects are "array-like" objects. Consider the code below:

    document.forms.length; // returns 1;
    document.forms[0]; // returns a form element.
    document.forms.join(", "); // throws a type error. this is not an array.
    typeof document.forms; // returns "object"
    

    Modifying your code, you could achieve this same affect:

    var a = 
    {
        0: 'a',
        1: 'b',
        2: 'c',
        length: 3,
        method: function() {
            return "test";
        }
    };
    

提交回复
热议问题