admin管理员组文章数量:1356758
If you do console.log($('some selector'))
in the browser, it returns what looks like an array (first line):
But notice that it's not an instanceof Array
, but it's actually the jQuery
object.
When you do console.dir($('h1'))
, it shows it's actually the jQuery object.
The question is, how are they making it look like it's an array in the web console? I noticed in the jQuery source here they add reference to a few Array and Object methods, and here they add toArray
(and slice and others) to the jQuery
object. Is the web console somehow checking for these methods and if it finds one (toArray
, indexOf
, slice
, etc.), it prints it as an Array? I would like to get this behavior out of any custom object, such as the Ember.ArrayProxy
. Currently when you log the Ember.ArrayProxy
it shows > Object
or whatever, but it would be nice to show it as an array.
Any ideas?
If you do console.log($('some selector'))
in the browser, it returns what looks like an array (first line):
But notice that it's not an instanceof Array
, but it's actually the jQuery
object.
When you do console.dir($('h1'))
, it shows it's actually the jQuery object.
The question is, how are they making it look like it's an array in the web console? I noticed in the jQuery source here they add reference to a few Array and Object methods, and here they add toArray
(and slice and others) to the jQuery
object. Is the web console somehow checking for these methods and if it finds one (toArray
, indexOf
, slice
, etc.), it prints it as an Array? I would like to get this behavior out of any custom object, such as the Ember.ArrayProxy
. Currently when you log the Ember.ArrayProxy
it shows > Object
or whatever, but it would be nice to show it as an array.
Any ideas?
Share Improve this question asked May 26, 2012 at 2:04 Lance PollardLance Pollard 79.6k98 gold badges332 silver badges609 bronze badges 1- Related: stackoverflow./questions/6599071/…. – pimvdb Commented May 26, 2012 at 12:40
1 Answer
Reset to default 15You make your object inherit Array
using the prototype, like so:
function SomeType() {
this.push(16);
}
SomeType.prototype = [];
SomeType.prototype.constructor = SomeType; // Make sure there are no unexpected results
console.log(new SomeType()); // Displays in console as [16]
And, of course, all jQuery objects are instances of the jQuery
function/constructor, so that's how jQuery does it. As a bonus, because of the inheritance, you get all the methods from Array
, and the indexing that es with it too!
本文标签: javascriptHow is (39h139) logging to the web console as an array in jQueryStack Overflow
版权声明:本文标题:javascript - How is $('h1') logging to the web console as an array in jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744005985a2574708.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论