2

Possible Duplicate:
Why jQuery do this: jQuery.fn.init.prototype = jQuery.fn?

I am reading through the jquery source and am confused early on. As it is, the jQuery function returns a "new jQuery.fn.init(selector,context);". Then jQuery.fn = jQuery.prototype, which contains the init function. Finally, jQuery.fn.init.prototype = jQuery.fn.

Main question: Why did the designers not just have the jQuery function return "new jQuery.fn(selector,context)" and have init and the other functions in jQuery.prototype (i.e. jQuery.fn) be in jQuery.fn.prototype?

Secondary concern: I don't see the point in setting jQuery.prototype at all considering you never call "new jQuery(s,c)"... the jQuery (i.e. $) object is just a wrapper on jQuery.fn to hide calling "new" in code, since it is prettier to say "$('div').foo()" than "new $('div').foo()"

Community
  • 1
  • 1
John Smith
  • 59
  • 2
  • Someone voted to close, giving the reason *"Not a real question."*. I'd say these are very good questions regarding the philosophy behind jQuery's inner workings. – user113716 Aug 12 '10 at 20:51
  • 2
    I voted to close as a duplicate, because it's dead on for the other question...but "not a real question"...who voted to close for that reason? – Nick Craver Aug 12 '10 at 21:05
  • The question comes off as "why didn't they do it my way?". As someone who's 20 lines into the source and just posted to second-guess some of the best Javascript out there. Perhaps i was a bit hasty, but reading over it, it still has that feel about it. – cHao Aug 12 '10 at 21:19
  • you were a bit hasty. Please keep the meta comments to a minimum and focus on helping. Thought I should explain why I flagged you. – John Smith Aug 12 '10 at 21:58
  • Nah...i was right the first time. Thanks for confirming that for me. – cHao Aug 12 '10 at 22:26

0 Answers0