Both jQuery and MathBox have query functions ($()
and select()
) which return objects with methods attached which act on all queried entities. E.g. $().addClass()
.
Does this pattern have a name?
I've always heard jQuery API style being called "Fluent API", or as Wikipedia says: "Fluent Interface".
Check https://en.wikipedia.org/wiki/Fluent_interface#JavaScript
However, it's mainly about chaining methods, usually to build queries or transformation chains. What the methods do and how they act on the handled entities depends on the application domain.
For the case of jQuery, it's similar to a composite pattern: an action can be applied on a single entity or a collection of them transparently, using the same interface. The difference with the classic Composite design pattern is that we're not talking about pure OOP, but about something closer to functional programming where returned values always forward the API, though with a new state.