Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do people call the jQuery $ alias a 'factory'?

Tags:

jquery

On various sites, jQuery's $ variable is referred to as an alias, and on others, it is referred to as a factory. I took a look at the source code, and I think the former is correct. As far as I can see, the dollar symbol is being defined here:

// Expose jQuery to the global object
return (window.jQuery = window.$ = jQuery);

This is setting both $ and jQuery to the SAME alias; there is no 'factory' for jQuery objects. Are the sites referring to $ as a 'factory' simply wrong?

like image 554
Jez Avatar asked Oct 28 '10 08:10

Jez


2 Answers

I think either is an okay term. The dollar sign is certainly an alias for the jQuery function, specifically to be used as a shorthand. If someone refers to the function as a factory, I don't think that's specific to the $ alias, but just describing what the jQuery function does in general, which is create objects from various different types of input.

like image 97
Jimmy Avatar answered Sep 29 '22 18:09

Jimmy


The jQuery function (on the right of the assignment in your question) is a factory (it creates new jQuery objects). It is not in the global scope, because it is defined in the scope of a self-executing function.

window.$ and window.jQuery are global aliases for the constructor.

like image 20
lonesomeday Avatar answered Sep 29 '22 17:09

lonesomeday