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

The Archive Base Latest Questions

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

Possible Duplicate: how does jquery chaining work? This is a normal thing you would

  • 0

Possible Duplicate:
how does jquery chaining work?

This is a normal thing you would see in a jQuery code:

$("div.selected").html("Blah.");

So, in the above code, in the function $(), it has a function called html(). And what I don’t understand is, what I normally will do is:

funcA("blah");  //normal function, cool.
funcA.funcB("blah");  //normal function in an object, still cool.

and now this is confusing:

funcA("blah").funcB("blah")  //huh??

How can funcB knows the arguments in funcA?
How can jQuery achieve this?

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-05-27T21:51:51+00:00Added an answer on May 27, 2026 at 9:51 pm
    //function returning an object is a possibility. bar has access to elem because of
    // the shared scope
    function foo ( elem ) {
        return {
            bar : function () {
                return elem.id;
            }
        };
    }
    

    In this one, the foo function returns an object containing whatever methods you wish. So when you call foo, you receive this:

    {
        bar : function () {
            return elem.id;
        }
    }
    

    elem is present from your call to foo. If you were to add a console.log( elem ) at the top of bar, you’d see that it’s the same thing as what you passed to foo.

    //this is kinda how jQuery does it:
    var foo = (function() {
        function foo ( elem ) {
            this.elem = elem;
        }
    
        foo.prototype.bar = function () {
            return this.elem.id;
        };
    
        return function ( elem ) {
            return new foo( elem );
        };
    }());
    

    This is a little more complex, and actually divided into two.

    function foo ( elem ) {
        this.elem = elem;
    }
    
    foo.prototype.bar = function () {
        return this.elem.id;
    };
    

    Who doesn’t love prototypical inheritance mixed with classical inheritance names? Anyway…functions act as both regular functions and as constructors. Meaning, when called with the new keyword, two special things happen:

    1. this inside of the foo refers to a freshly made copy of foo.prototype
    2. foo.prototype is returned (unless foo returns an object)

    Note that foo.prototype isn’t a magic value. It’s just like any other object property.

    So, inside the foo function/constructor, we’re merely setting foo.prototype.elem, but not directly. Think of it like this (a little inaccurate, but it’ll do): foo.prototype is the blueprint of a product. Whenever you wish to make more, you use the blueprint – duplicate what’s inside, pass it along. Inside of foo, this refers to such a replication of the blueprint.

    However, by explicitly setting values on foo.prototype, we’re altering the blueprint itself. Whenever foo is called, it’ll be called with this altered blueprint.

    Finally, once foo is finished, the replication (the duplicated blueprint, but after foo has done stuff with it) is returned. This replication contains the original blueprint, and everything else we might have added – in this example, elem.

    var foo = (function() {
        ...
        return function ( elem ) {
            return new foo( elem );
        };
    }());
    

    We create a nameless function and immediately execute it.

    (function () {
        console.log( 'meep' );
    }());
    
    //is the equivalent of:
    var something = function () {
        console.log( 'meep' );
    };
    something();
    something = undefined; //we can no longer use it
    
    //and of this
    function () {
        console.log( 'meep' );
    }(); //<--notice the parens
    //however, it's considered good practice to wrap these self-executing-anonymous-functions
    // in parens
    

    Like all other functions, they can return values. And these values can be captured into variables.

    var answer = (function () {
        return 42;
    }());
    answer ==== 42;
    
    var counter = (function () {
        var c = 0;
        return function () {
            return c++;
        };
    }());
    //counter is now a function, as a function was returned
    counter(); //0
    counter(); //1
    counter(); //2...
    

    So:

    var foo = (function () {
        ...
        return function ( elem ) {
            ...
        };
    }());
    

    Returns a function which receives an argument, and that function is now assigned to foo.

    The insides of the function:

    return new foo( elem );
    

    Is to ensure the special conditions I’ve spoken about above – it ensures that a new copy of the blueprint is manufactured, by explicitly doing the new operation. This can actually be replicated like this:

    function foo ( elem ) {
        this.elem = elem;
    }
    foo.prototype.bar = function () {...};
    

    As long as you always call foo with the new keyword.

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

Sidebar

Related Questions

Possible Duplicate: How does this work? Weird Towers of Hanoi Solution While surfing Google,
Possible Duplicate: What does ‘unsigned temp:3’ means I just found this code in a
Possible Duplicate: Can someone Explain this jQuery code? I have posted this before, but
Possible Duplicate: nearest ancestor node in jQuery html: <div class=post> <h2><a href=# title=>Pellentesque</a></h2> <p><a
Possible Duplicate: What does this JavaScript/jQuery syntax mean? I specifically mean when you do
Possible Duplicate: What does (function($) {})(jQuery); mean? I've seen a lot of jQuery code
Possible Duplicate: In jQuery, what does $.fn. mean? Can someone explain the code below
Possible Duplicate: Does HTML5 require clients to support JavaScript? Would HTML5 API's work when
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: What does () => mean in C#? Hi Everybody, This is my

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.