Shivam
Shivam

Reputation: 2248

What is the difference between $() and $.?

I would like to figure out the difference between using $() and $. from other developers.

As far as my understanding goes, $() refers to objects within the DOM, but I am not 100% clear as to how the $. works. I have used the $. format before, but never understood how it works.

For example:

$.each(element, function() {}); or $.fn etc...

It would be nice to shed some light and clarity on this topic.

Upvotes: 3

Views: 4125

Answers (5)

losnir
losnir

Reputation: 1171

Basically, $() is a constructor function - you pass it a DOM selector, and it returns a jQuery object.

On the other hand, $.each $().each or $. $(). is a prototype function of jQuery.

Edit:

I stand corrected by @apsillers: $().each is a prototype functions, but $. (i.e. $.each) are defiend directly on jQuery, or $.

Code sample from jQuery to back up my claim:


jQuery.fn = jQuery.prototype = {
   .....
   .....
   .....
   // Execute a callback for every element in the matched set.
   // (You can seed the arguments with an array of args, but this is
   // only used internally.)
   each: function( callback, args ) {
      return jQuery.each( this, callback, args );
   },
   ....
   ....
};

As you can see, $().each is a prototype function. jQuery.each is an internal method. defined directly on jQuery.

Taken from jQuery Source Code.

Upvotes: 5

apsillers
apsillers

Reputation: 115940

$() creates an jQuery instance object that holds some list of elements. Methods

$ is the one-and-only jQuery object, that has some non-instance-specific methods. (It may be helpful to think of them as "static" methods, to borrow terminology from other languages).

This one-and-only jQuery object, identified by $, is a function (which is why it is callable as $()). However, functions in JavaScript are objects, so they can have properties and member functions themselves.

Upvotes: 5

Will
Will

Reputation: 7017

$ is the jQuery object while $() is an evaluation of that object (which is also a function).

The meaning of $() is (from http://api.jquery.com/jQuery/):

Return a collection of matched elements either found in the DOM based on passed argument(s) or created by passing an HTML string

Upvotes: 2

Rory O'Kane
Rory O'Kane

Reputation: 30388

$ is just a variable with a short name that points to the same thing as jQuery. $() is the same as jQuery() (calling jQuery as a function), and $.foo is the same as jQuery.foo. jQuery.foo accesses the foo property of the jQuery object – function objects can have other properties too.

Upvotes: 2

Quentin
Quentin

Reputation: 943216

$ is an identifier. It is used as a variable. It has a function assigned to it.

Putting () after a function will call it. The function jQuery assigns to it does lots of different things depending on what sort of arguments you pass to it. (It is horribly overloaded). (e.g. if you pass it a function, it will call that function when the document ready event fires. If you pass it a string of HTML, it will create a DOM representation of that HTML and wrap it in a jQuery object. If you pass it a DOM node, it will wrap that node in a jQuery object. If you pass it a CSS selector, it will search the document for matching DOM nodes and wrap them with a jQuery object).

In JavaScript, functions are objects. Objects can have properties. You can access a property on an object via $.name_of_property. Those properties can also have functions (or other objects) assigned to them.

Upvotes: 13

Related Questions