Difference of the value, prototype and property

I don’t like that pattern either. They have an init function, which is the constructor of all jQuery instances – the jQuery function itself is just a wrapper around that object creation with new:

function jQuery(…) { return new init(…); }

Then, they add the methods of those instances to the init.prototype object. This object is exposed as an interface at jQuery.fn. Also, they set the prototype property of the jQuery function to that object – for those who don’t use the fn property. Now you have

jQuery.prototype = jQuery.fn = […]init.prototype

But they also do two [weird] things:

  • overwriting the constructor property of the prototype object, setting it to the jQuery function
  • exposing the init function on jQuery.fn – its own prototype. This might allow Extending $.fn.init function, but is very confusing

I think they need/want to do all this to be fool-proof, but their code is a mess – starting with that object literal and assigning the init prototype things afterwards.

Leave a Comment