What makes a jQuery object show up as an array in Chrome's Developer Tools?

前端 未结 2 1968
旧时难觅i
旧时难觅i 2020-11-27 22:48

I\'m wondering how it\'s possible that jQuery objects show up as an array in the console log of Developer Tools in Chrome.

E.g. if I execute $(\'\')

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 23:33

    From http://api.jquery.com/jQuery.makeArray/:

    Many methods, both in jQuery and in JavaScript in general, return objects that are array-like. For example, the jQuery factory function $() returns a jQuery object that has many of the properties of an array (a length, the [] array access operator, etc.), but is not exactly the same as an array and lacks some of an array's built-in methods (such as .pop() and .reverse()).

    Basically, The object has to have length and splice properties to be array-like. Here is a relevant SO question: Array Like Objects in Javascript

提交回复
热议问题