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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:36:20+00:00 2026-05-18T00:36:20+00:00

i’m reading through jQuery’s Plugins/Authoring though i already wrote a few jQuery-Plugins. Now I

  • 0

i’m reading through jQuery’s “Plugins/Authoring” though i already wrote a few jQuery-Plugins. Now I see that jQuery has a special way of scoping the methods and calling:

(function( $ ){

  var methods = {
    init : function( options ) { // THIS },
    show : function( ) { // IS   },
    hide : function( ) { // GOOD },
    update : function( content ) { // !!! }
  };

  $.fn.tooltip = function( method ) {

    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }    

  };

})( jQuery );

I understand the concept of what will happen in the end… but how exactly? This part is what confuses me:

    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    }

Why Array.prototype.slide.call(argumetns, 1)? And where does the variable “arguments” come from all of the sudden? Any brief or deeper explanation is much appreciated. It is said, that this is how plugins should be written… so i’d like to know why.

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-18T00:36:21+00:00Added an answer on May 18, 2026 at 12:36 am

    arguments

    arguments is a part of the JavaScript language. I was confused in exactly the way you were when I first ran into it; it’s not just you. 🙂 It’s an automatic local variable in every function, and is an array-like structure giving you all of the arguments (see Section 10.6 of the spec), e.g.:

    function foo() {
        var index;
    
        for (index = 0; index < arguments.length; ++index) {
            alert(arguments[index]);
        }
    }
    foo("one", "two"); // alerts "one", then alerts "two"
    

    When I say arguments is array-like, I mean it — it’s not an Array. Its references to the arguments are live (and bidirectional). For instance:

    function foo(namedArg, anotherNamedArg) {
        alert(namedArg === arguments[0]);        // alerts true, of course
        alert(anotherNamedArg === arguments[1]); // also alerts true
        namedArg = "foo";
        alert(arguments[0]);                     // alerts "foo"
        arguments[0] = "bar";
        alert(namedArg);                         // alerts "bar"
    }
    

    Note that when assigning a value to namedArg, the result is reflected in arguments[0], and vice-versa.

    arguments is really cool, but only use it if you need to — some implementations speed up calling functions by not hooking it up until/unless the function actually first tries to access it, which can slow the function down (very slightly).

    arguments also has property on it called callee, which is a reference to the function itself:

    function foo() {
        alert(foo === arguments.callee); // alerts true
    }
    

    However, it’s best to avoid using arguments.callee for several reasons. One reason is that in many implementations, it’s really slow (I don’t know why, but to give you an idea, the function call overhead can increase by an order of magnitude if you use arguments.callee). Another reason is that you can’t use it in the new “strict” mode of ECMAScript5.

    (Some implementations also had arguments.caller — shudder — but fortunately it was never widespread and is not standardized anywhere [nor likely to be].)

    The slice call and apply

    Regarding

    return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    

    What that’s doing is using the Array#slice method to copy the arguments into an array (minus the first argument, which was the method to call), and then passing the resulting array into the Function#apply function on the function instance it’s calling. Function#apply calls the function instance with the given this object and the arguments supplied as an array. The code’s not just using arguments.slice because (again) arguments isn’t really an Array and so you can’t rely on it having all of the Array functions, but the specification specifically says (in Section 15.4.4.10) that you can apply the Array.prototype.slice function to anything that’s array-like, and so that’s what they’re doing.

    Function#apply and Function#call are also built-in parts of JavaScript (see Sections 15.3.4.3 and 15.3.4.4). Here are simpler examples of each:

    // A function to test with
    function foo(msg, suffix) {
        alert(this.prefix + ": " + msg + suffix);
    }
    
    // Calling the function without any `this` value will default `this`
    // to the global object (`window` on web browsers)
    foo("Hi there", "!"); // Probably alerts "undefined: Hi there!" because the
                          // global object probably doesn't have a `prefix` property
    
    // An object to use as `this`
    var obj = {
        prefix: "Test"
    };
    
    // Calling `foo` with `this` = `obj`, using `call` which accepts the arguments
    // to give `foo` as discrete arguments to `call`
    foo.call(obj, "Hi there", "!"); // alerts "Test: Hi there!"
          // ^----^-----------^---- Three discrete args, the first is for `this`,
          //                        the rest are the args to give `foo`
    
    // Calling `foo` with `this` = `obj`, using `apply` which accepts the arguments
    // to give `foo` as an array
    foo.apply(obj, ["Hi there", "!"]); // alerts "Test: Hi there!"
                // ^---------------^---- Note that these are in an array, `apply`
                //                       takes exactly two args (`this` and the
                //                       args array to use)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.