-3

I've been wondering this for a while now, when searching around for an answer, all I find is jQuery tutorials. I already know how to use it, I want to know what makes it tick.

For example, things like chaining functions together

$('#id').show().animate().hide();

How does that work, how can I recreate it myself in JavaScript?

I've looked at the jQuery source code and cannot figure it out, I can understand some parts, but others are confusing. Then the uncompressed developer version is 10,000 lines long!

I want to have a go at creating my own JavaScript library, it may never be as good as jQuery, but the amount I will learn about both JavaScript and jQuery will make me better at using both. Until I have a good understanding of how it works, the question is where to start? And the answer to that question is... ask questions!

  • Consider what happens if you have one large object on which you add a bunch of functions. As long as each function in turn returns that same object, you can chain them all you like. Each function would be called in sequential order before invoking the next. – David Jul 06 '15 at 20:10
  • 1
    "*And the answer to that question is... ask questions*" That's one way. Personally I prefer the _try, fail, google, try again, fail, ask specific question, succeed_ :) I feel that you learn more from trial and error than anything else, and a bonus is that your questions will be much more specific once you start digging. – Mackan Jul 06 '15 at 20:16

1 Answers1

0

JQuery is based around the idea of the jQuery object, a javascript object that wraps a list of DOM elements and can perform a variety of functions on that list.

The key to your question is that most of the functions that can be run on the jQuery object return the jQuery object (or a new jQuery object). This is what permits the chaining. (If you examine the jQuery API documentation, each function lists the return type, which is generally "jQuery".)

Jacob Mattison
  • 50,258
  • 9
  • 107
  • 126