jQuery $.each and $.fn.each

In jQuery, there are mainly two namespaces –

1) jQuery.fn namespace ( $.fn namespace ) and

2) jQuery namespace ($ namespace )

$ sign is an alias of jQuery (also known as Hungarian notation). This $ can be returned to its original i.e. jQuery using jQuery.noConflict(). This is used when $ variable/ sign is being used by some other library. Also sometime, it is used to handle different versions of jQuery on the same page.

Each namespace has its own context.

$ Namespace:

$ Namespace is mainly focused on utilities functions and its methods are known as core methods. These core methods do not work with selections. 

Ex. $.proxy(), $.each(), $.extend(), $.inArray() etc.

For example  

$.trim('   c-sharpcorner.com   '); 

$.fn namespace:

$.fn namespace (also knowns as jQuery prototype) focus on methods that works on jQuery objects and these methods are referred as jQuery object methods. All methods used on this fashion or jQuery Object Methods - $(expressionForSelection).allMethods()

For example  

$('div[id$=myDivID]').css('color','green').show(); 

So, $.each is a jQuery's core method where $.fn.each is a jQuery Object Method. Later is works on selection (of DOM elements that is wrap into JavaScript object by jQuery). The first one ($.each) works directly or we can say globally with jQuery without any selector.

Another thing is that jQuery object method returns jQuery object while core methods not(also its return type is differ on case to case).

Ebook Download
View all
Learn
View all