6

I am looking into how jQuery source code works, I understand the jQuery object just forwards a call to jQuery.fn.init where jQuery.fn is just a reference to jQuery.prototype.

Then in the source code, there is this line:

// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;

There is a comment to explain what the code does but I still can't understand it.

  1. Can someone explain what does this line of code means? what later instantiation is it talking about and why do we need to set init's prototype to jquery's prototpe?

  2. is there a reason (like avoiding conflicts or readability or whatever) that jQuery source code is using jQuery.fn instead of using jQuery.prototype directly?

1 Answer 1

2

(This response is written assuming you have some understanding of prototypal inheritance. If you don't, you need to read an article about it to fully understand what's going on. Try doing a Google search for "prototypal inheritance javascript".)

When a new jQuery object is created internally, it is created with new jQuery.fn.init(). init is a constructor, so setting the prototype property on this constructor allows newly created jQuery objects to inherit all the properties of this prototype (all the methods of jQuery.fn).

If just new jQuery() was used, as you seem to suggest, the object would inherit from jQuery.prototype but the jQuery function would be executed, which as you know does a lot. The init constructor is used instead because it doesn't come with the baggage of the jQuery function. Setting jQuery.prototype to the same as jQuery.fn.init.prototype just allows you to do jqueryobject instanceof jQuery, which is nice, so that's the reason the jQuery object has a prototype.

3
  • 1
    Only functions have a prototype property. All objects have an internal [[Prototype]], but that's a different thing. Commented Nov 18, 2012 at 14:26
  • I'm not sure what you mean when you say the jQuery function does a lot. The jQuery function simply calls jQuery.fn.init, one line. Setting jQuery.fn.init.prototype to jQuery.fn isn't just so you can do an instanceof. Because jQuery() calls new jQuery.fn.init(), the init constructor needs a prototype, and preferably one with all the jQuery methods. All the jQuery methods are on the jQuery.fn object, and so these are added to the init prototype. This step also allows the instanceof check. This may have been different at the time of writing - just wanted to clarify Commented Nov 3, 2013 at 22:06
  • @NathanWall Hi, im looking for the snippet where jQuery(html, attributes) is defined in the source code. api.jquery.com/jQuery/#jQuery2 In your answer you mentioned "all the methods of jQuery.fn" .. it's got to have a ton of overloads for the jQuery() function, can you maybie point me at a GitHub file where i can find these overloads?
    – philx_x
    Commented Mar 31, 2015 at 14:59

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.