Why console don't show methods of jQuery object returned from selector?

北慕城南 提交于 2019-12-02 21:15:51

问题


I know that if I type:

$('body');

I get a jQuery object. However on chrome's console I'll only see the internal array of the jQuery object, despite the fact that jQuery methods are accessible like

$('body').hide();

Why the console don't show me all the accessible methods and how did jQuery manage to do this magic?

If it's just because these methods are defined on a prototype, then how come when I write these lines:

function Person(){this.myProp = 'test'};
var person = new Person();
Person.prototype.proto = 'test2';

and then I inspect person in chrome I will see:

__proto__: Person constructor: Person() proto: "test2"

but when Inspecting $('body'); then no proto is shown on dev tools?


回答1:


The methods are on the prototype of the object. Both the console and console.log() do not, by default, show items on the prototype.

If you examine a jQuery object in the Chrome debugger, you can expand the prototype and can then see all the methods there.

So, what you are seeing is just the chosen implementation of the console. It shows direct instance properties, not items on the prototype.


When I put this code into a page:

function Person(){this.myProp = 'test'};
Person.prototype.proto = 'test2';

var person = new Person();
var jq = $("body");

And, then examine both person and jq in the Chrome debugger, I see this:

which shows the `proto property on both objects. And, if I expand that property for the jQuery object, it does indeed show all the methods.



来源:https://stackoverflow.com/questions/33246347/why-console-dont-show-methods-of-jquery-object-returned-from-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!