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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:14:26+00:00 2026-06-17T23:14:26+00:00

I read Jhon resig post about function overloading: http://ejohn.org/blog/javascript-method-overloading/ The functions: function Users(){ addMethod(this,

  • 0

I read Jhon resig post about function overloading:

http://ejohn.org/blog/javascript-method-overloading/

The functions:

function Users(){
  addMethod(this, "find", function(){
    // Find all users...
  });
  addMethod(this, "find", function(name){
    // Find a user by name
  });
  addMethod(this, "find", function(first, last){
    // Find a user by first and last name
  });
}

// addMethod - By John Resig (MIT Licensed)
function addMethod(object, name, fn){
    var old = object[ name ];
    object[ name ] = function(){
        if ( fn.length == arguments.length )
            return fn.apply( this, arguments );
        else if ( typeof old == 'function' )
            return old.apply( this, arguments );
    };
}

I understand the concept. The one thing I can’t understand is why each time the else if statement is executing the return old.apply(this, arguments) the arguments length is decremented by one.

I used alerts to follow the function and that is the main thing I can’t understand.

Any assistance will be appreciated.

  • 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-17T23:14:27+00:00Added an answer on June 17, 2026 at 11:14 pm

    It effectively results in a chain of old functions that are called one after the other until one whose arity matched arguments.length is found. So on the first call to addMethod, you’ll end up with old being undefined.

    The 2nd time you call addMethod, old will end up being the function that was assigned in during the first call.

    If you run this:

    var users = new Users();
    users.find("James");
    

    It will look for a property of users with the identifier find. It will find one, and the arity of that function will be 2 (because the final call to addMethod in the constructor creates a method that expects 2 arguments). So instead of calling that, it calls old, which will be the version of the method that expects 1 argument. Our invocation has a single argument, and that matches, so that’s the method that gets executed.

    If you run this:

    users.find();
    

    It will do exactly what I described above, but when it calls old the arity will not match and it will call old again. The value of old in this situation will be what it was after the first call to addMethod (that’s a demonstration of a thing usually referred to as a closure – the old old function is still available after the parent function has returned).

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

Sidebar

Related Questions

Read some texts about locking in PHP. They all, mainly, direct to http://php.net/manual/en/function.flock.php .
Read EDIT 2 at bottom Original post: I have this jQuery validator method to
I read a blog post at one time on how to increase the amount
I read Jeff's blog post that someone had managed to see a closed source
Read about Fog: http://fog.io Does Python have anything like it? (a library for cloud
Read about Server push here . I want to push data to client from
Read about the issue in this stackoverflow question . Still have the same issue
I read an article about a regular expression to detect base64 but when I
Read about the z-index issue and tried anything, but just can't seem to fix
Read this question today about safe and unsafe code I then read about it

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.