Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7005037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:16:40+00:00 2026-05-27T21:16:40+00:00

jQuery or $ seems to be a function: typeof $; // function And it

  • 0

jQuery or $ seems to be a function:

typeof $; // "function"

And it acts like one:

$('div').removeClass(); // $ constructs a new object with some methods like removeClass

But when I drop the function parentheses it behaves like an object:

$.each(/* parameters */); // $ is an object with some methods like each

I’d like to know how this is possible and how I can implement this behaviour to my own functions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:16:41+00:00Added an answer on May 27, 2026 at 9:16 pm

    Functions are also objects, so $.each can be defined in a similar way as an Object.

    JavaScript is a prototypical language. For jQuery, this means that every instance of $ inherits methods from jQuery.prototype.See Notes

    A very rough demo, to achieve the similar behaviour:

    (function() { // Closure to not leak local variables to the global scope
        function f(a, b) {
            //Do something
        }
        // Prototype. All properties of f.prototype are inherited by instances of f.
        // An instance of f can be obtained by:    new f, new f(), Object.create(f)
        f.prototype.removeClass = function(a) {
            return a;
        };
        function $(a, b) {
            return new f(a, b); // <--- "new f" !  
        } 
        $.each = function(a) {
            alert(a);             
        };
        window.$ = $; // Publish public methods
    })();
    
    //Tests (these do not represent jQuery methods):
    $.each("Foo");                   // Alerts "Foo" (alert defined at $.each)
    alert($().removeClass('Blabla'));// Alerts "Blabla"
    

    Notes

    jQuery’s root method is defined as follows (only relevants parts are shown):

    (function(win) {
        var jQuery = function (selector, context) {
            return new jQuery.fn.init(selector, context, rootjQuery);
        };
        //$.fn = jQuery.fn is a shorthand for defining "jQuery plugins".
        jQuery.fn = jQuery.prototype = {
            constructor: jQuery,
            init: function( /* ..parameters.. */ ) { 
                //.... sets default properties...
            }
            //....other methods, such as size, get, etc...
            //.... other properties, such as selector, length, etc...
        };
        jQuery.fn.removeClass = function() { // (Actually via jQuery.fn.extend)
            // ... method logic...
        };  //...lots of other stuff...
    
        win.$ = win.jQuery = jQuery; //Publish method
    })(window);
    

    The advantage of the prototype method is that it’s very easy to chain methods and properties. For example:

    $("body").find("div:first").addClass("foo");
    

    A method to implement this feature could be:

    $.fn.find = function(selector) {
        ...
        return $(...);
    };
    

    If you’re interested in jQuery’s real implementation, have a look at the annotated source code:

    • jQuery core – Definitions of the constructor and base methods.
    • jQuery.fn.extend is used to add removeClass, etc. to jQuery.
    • jQuery 1.7.1.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that i cannot access $(this) inside jquery ajax success function. please see
It seems like this should be something built into jQuery without the need for
seems like I'm stuck with jQuery tabs. I'm trying to pass selected tab name
My code worked fine in JQuery 1.3.2, but in 1.4.2 it seems to be
hello im trying to make something like github treeslider im new to javascript.. but
I defined my plugin base on http://docs.jquery.com/Plugins/Authoring (function( $ ){ var methods = {
I want to write a function like jQuery each and am using the javascript
jQuery's getScript function doesn't seem to support an error callback function. I can't use
I have a table column I’m trying to expand and hide. jQuery seems to
I'm trying to filter a list of elements via ':not()', and jQuery seems to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.