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

  • SEARCH
  • Home
  • 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 8753521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:26:29+00:00 2026-06-13T13:26:29+00:00

As an example, here is a screenshot from jQuery1.8. Why can I do something

  • 0

As an example, here is a screenshot from jQuery1.8.
enter image description here

Why can I do something like this from the perspective of jQuery source code ?
Is addClass jquery’s method? But what i saw was from jQuery.fn.extend.

$("xid").addClass("xxx");

I am relatively new to JavaScript and wrote some pretty simple code like directly add a method in .html.

 <html>
    <script>
        function m() {
            alert("simple");
        }
    </script>
 </html>

I couldn’t understand the complex syntax particularly in the JavaScript Framework like ExtJs,jQuery, although I have searched a number of relative documents about this, but I still don’t have a clear understanding. Maybe I need to spend time seriously reading a special JavaScript book, but this moment I want to know the answer.

Thanks.

  • 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-06-13T13:26:30+00:00Added an answer on June 13, 2026 at 1:26 pm

    There are several layers to this question. The first is object literals. Javascript allows you to declare objects using object literal syntax (you may have heard of JSON which is just the syntax stored/transferred in a string). The syntax is as follows:

    var obj = { key1 : "value1", key2 : "value2" };
    

    A more structured/readable way to write this is:

    var obj = {
      key1 : "value1",
      key2 : "value2"
    };
    

    The code above is exactly the same as doing:

    var obj = new Object();
    obj.key1 = "value1";
    obj.key2 = "value2";
    

    So what you’re seeing in the line:

    addClass : function () {
    

    is merely a part of the object literal declaration. Essentially the code is doing this:

    var tmp = new Object();
    // ...
    tmp.addClass = function () { /* ... */ };
    // ...
    jQuery.fn.extend(tmp).
    

    Except that it declares the object literal directly in the call to extend() so it is not necessary to use a temporary variable.

    The second thing is functions in javascript are not special. What I mean is, functions are treated the same as everything else: strings, numbers, arrays & objects. In fact, in javascript, strings, numbers, arrays and functions are all objects. So you can assign a function to a variable:

    var n = function () {alert("Hello World");}
    

    So there is nothing magical happening at all. It’s simply creating an anonymous object in object literal syntax that contains a collection of functions that is passed as an argument to the extend method.

    That is the explanation of the syntax. That is, what is happening in javascript per se.

    We know that we’re looking at jQuery code. So that gives us more clues as to what’s happening. First thing first: jQuery.fn is simply a pointer to jQuery.prototype. And jQuery itself is a constructor. In javascript constructors are regular functions that can be used to create objects. When you create an object with a constructor, that object inherits from the constructor’s prototype.

    And jQuery has modified its prototype and added an extend method which allows you to add other methods and attributes to jQuery’s prototype. Which is what it’s doing. All the functions contained in the object literal mentioned earlier will be processed by the extend method to be added to jQuery itself so that when you create new jQuery objects they’ll inherit them.


    Based on your comment it looks like there are lots of things about javascript you need to learn. May I suggest reading through the Rhino book: “JavaScript: The Definitive Guide”. And once you know more and are more comfortable with javascript I’d also suggest reading “Javascript: The Good Parts”.

    I’ll try to answer the best I can here about your comment. To explain everything requires a whole book.

    First, about the line:

    jQuery.extend = jQuery.fn.extend = function() {
    

    As mentioned before, jQuery.fn is just a pointer to jQuery.prototype. So what it’s doing is basically this:

    jQuery.prototype.extend = function () { /* ... */ };
    jQuery.extend = jQuery.prototype.extend;
    

    What’s the difference between the two? The first thing you need to remember is that jQuery is a constructor. An object that is created by a constructor inherits it’s prototype (javascript don’t have classes but switch the word “prototype” with “class” and you begin to get an understanding of how objects work). So, the first declaration of extend defines it as a method for all instances of jQuery objects:

    // note: The following is not the recommended way to write jQuery code
    //       but it does illustrate the point about inheritance:
    
    var foo = new jQuery();
    foo.extend(); // this works because foo inherits jQuery's extend method
    

    Now, remember I said that in javascript functions are also objects? The second declaration treats the jQuery function as an object and adds a function directly to it. This is mostly syntax sugar. It’s so that you can write:

    jQuery.extend();
    

    instead of the longer:

    jQuery.prototype.extend();
    

    or something convoluted like:

    new jQuery().extend.call(jQuery);
    

    depending on how extend was written.

    So, jQuery.extend() is simply a method of the jQuery constructor itself. It is not inherited by objects created by the jQuery constructor. Think of it as a class method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You can see the example here: http://jsfiddle.net/8EHED/8/ This is a tricky problem because I
I use ffmpeg to capture screenshot from video. Here is the command code: ffmpeg
The fictional example here. Suppose I designed a class named IODevice. This class already
Take a look at this example here: http://denise.brixwork.com/showlisting/1/8297-Valley-Drive-Alpine-Meadows-Whistler-denise-brown-real-estate And the red tables under Specifications
I copied the Google Maps AutoComplete JS example from the demo here: Places Map
I have some code working perfectly on a specific source solution which references jQuery
Example: here is the string: blablabla123:550:404:487blablabla500:488:474:401blablablabla here is what I'm using: string reg =
Example here: http://jezebel.com/5896408/racist-hunger-games-fans-dont-care-how-much-money-the-movie-made Click on the 3x3 Tweets screencap they have up. I love
I found an example here to create a select list with optgroups using KnockoutJS.
Checking the datagrid example here http://www.silverlight.net/content/samples/sl4/toolkitcontrolsamples/run/default.html see the datagrid section It says that if

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.