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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:21:59+00:00 2026-06-17T04:21:59+00:00

Following the jQuery plugin pattern , how can one find the arity of a

  • 0

Following the jQuery plugin pattern, how can one find the arity of a function, say the methods.myfunc function, in the case where we’ve used apply() to define the scope of this and applying arguments to this?

(function($, window, document ){

"use strict";
//...
methods = {
  myfunc: function(){
    // myfunc.length? tried, didnt work
    // arguments.length? tried, didnt work
    // methods.myfunc.length? tried, didnt work
    // arguments.callee tried, doesnt work in strict mode
  }
  //...
}

$.MyPluginThing = function( method ){

  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, window );
  }else{
      $.error( "Method " +  method + " does not exist on jQuery.MyPluginThing" );
  }

}...

This may expose some of my ignorance with function scope, but I’m pretty stumped here, and have not found an example that explains this well enough.

Part of my inspiration for this question comes from NodeJS/ExpressJS, where they have a variable number of arguments for some functions. If 3 arguments are passed, for example, it’s assumed there is an error object, but you could just as easily pass two and there’s no problem!

update: changed function in code from init to myfunc

  • 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-17T04:22:00+00:00Added an answer on June 17, 2026 at 4:22 am

    You’ll have to use a named function expression (with all its idiosyncrasies):

    var methods = {
      init : function init () {
        var arity = init.length;
      }
    };
    

    Here’s the fiddle: http://jsfiddle.net/tqJSK/

    To be honest though, I have no idea why you’d ever need this. You can hard code that number within the function, since the amount of named arguments will never change…


    Update: as pointed out by @T.J.Crowder, you can use regular function declarations instead:

    (function($, window, document) {
    
        function init () {
            var arity = init.length;
        }
    
        var methods = {
          init : init
        };
    
    }(jQuery, window, document));
    

    Update 2: if all you’re looking for is the number of arguments supplied in this specific call, just use arguments.length:

    var methods = {
      init : function () {
        var count = arguments.length;
      }
    };
    

    Here’s the fiddle: http://jsfiddle.net/tqJSK/1/

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

Sidebar

Related Questions

The following jQuery plugin has an API where you can add your own methods.
Say I have the following jQuery plugin: $.fn.myPlugin = function () { //plugin code
I have the following home-grown jquery plugin: (function($) { $.fn.defaultButton = function(button) { var
I am looking fo ra jQuery plugin that does the following behaviour: 1. Find
I've got the following jsp page and I'm using struts2-jquery-plugin-3.1.1 <script type=text/javascript> function openDialog()
I'm writing a jquery plugin using the following pattern (because I don't want to
I am using jQuery Validation Plugin (http://bassistance.de/jquery-plugins/jquery-plugin-validation/) I have wrote following rule jQuery.validator.addMethod(valueNotEquals, function(value,
I'm using the following jquery multiselect plugin ... How can I grab the selected
I'm using the following jquery multiselect plugin ... How can set the selectedText as
All, I'm using the following jQuery plugin: https://github.com/blueimp/jQuery-File-Upload You can see that in the

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.