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 6029991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:58:12+00:00 2026-05-23T04:58:12+00:00

How is it that jQuery can do $(#foo).addClass(bar) and $.ajax() ? I’m creating a

  • 0

How is it that jQuery can do $("#foo").addClass("bar") and $.ajax()?

I’m creating a micro javascript framework and want to create a new instance of an object, such as $("#hello"). With this object there are associated methods, such as addClass, css, etc, just like with jQuery. So I could do something like

$("#foo").addClass("remove").css("color", "red");

I have been successful in creating this. However, when I want to call a method from this object, such as $.ajax, the constructor function is overwritten, and I can call $.ajax, but not $(“#foo”).

Basically, how can jQuery do both?

  • 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-23T04:58:13+00:00Added an answer on May 23, 2026 at 4:58 am

    OK, the $ function is not only a function but an object, like all functions. So it can have methods. That’s all that ajax is, a method of the $ function. So we can start off by doing this:

    $ = function(obj) {
      // some code 
    };
    $.ajax = function (arg1, arg2) {
      // some ajax-y code
    };
    

    So far so good. Now, what on earth do we put in the $ function? Well it has to return an object and that object has to have some nice methods defined on it. So we’ll need a constructor function (to give us new objects) and a prototype (to provide the nifty methods for those objects).

    $ = function(obj) {
      var myConstructor = function (obj) {
        this.wrappedObj = obj;
      };
    
      myConstructor.prototype = {
        niftyMethod: function () {
          // do something with this.wrappedObj
          return this; // so we can chain method calls
        },
        anotherNiftyMethod: function (options) {
          // do something with this.wrappedObj and options
          return this; 
        }
      };
    
      return new myConstructor(obj);
    };
    

    So there we have it. We can do this:

    var mySnazzObject = $("whatever");
    mySnazzObject.niftyMethod().anotherNiftyMethod(true);        
    

    And we can do this:

    $.ajax("overthere.html", data);
    

    Obviously jQuery does a heck of a lot more than that, and it does it in some really impressive ways, but that’s the general idea.

    UPDATE: AS @Raynos was kind enough to observe without supplying a constructive answer, my original code would create the prototype ad infinitum. So we make use of an anonymous autoexecuting function to declare the constructor and prototype separately:

    (function () {
      var myConstructor = function (obj) {
        this.wrappedObj = obj;
      };
    
      myConstructor.prototype = {
        niftyMethod: function () {
          // do something with this.wrappedObj
          return this; // so we can chain method calls
        },
        anotherNiftyMethod: function (options) {
          // do something with this.wrappedObj and options
          return this; 
        }
      };
    
      var $ = function(obj) {
        return new myConstructor(obj);        
      };
    
      $.ajax = function (arg1, arg2) {
        // some ajax-y code
      };
    
      window.$ = $;  
    }());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that jQuery.extend can be used to add by own custom methods to
I'm looking for a plugin for jQuery that can validate as a key is
Can jQuery be extended so that I can use the above syntax? I can't
How can I use jquery to remove a SPECIFIC div that has no children
How can I access options that I set in a jQuery Datepicker? $(#testDatePicker).datepicker({ minDate:
Is it possible that using jQuery, I cancel/abort an Ajax request that I have
I have a form that uses jQuery to submit an ajax post and it
I have this code in jQuery, that I want to reimplement with the prototype
Here's a little jQuery function that does what I want when a checkbox is
Is it correct to assume that JQuery is not actually an implementation of ECMA

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.