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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:01:37+00:00 2026-05-12T16:01:37+00:00

I have a static javascript function that can take 1, 2 or 3 parameters:

  • 0

I have a static javascript function that can take 1, 2 or 3 parameters:

function getData(id, parameters, callback) //parameters (associative array) and callback (function) are optional

I know I can always test if a given parameter is undefined, but how would I know if what was passed was the parameter or the callback?

What’s the best way of doing this?


Examples of what could be passed in:

1:

getData('offers');

2:

var array = new Array();
array['type']='lalal';
getData('offers',array);

3:

var foo = function (){...}
getData('offers',foo);

4:

getData('offers',array,foo);
  • 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-12T16:01:37+00:00Added an answer on May 12, 2026 at 4:01 pm

    You can know how many arguments were passed to your function and you can check if your second argument is a function or not:

    function getData (id, parameters, callback) {
      if (arguments.length == 2) { // if only two arguments were supplied
        if (Object.prototype.toString.call(parameters) == "[object Function]") {
          callback = parameters; 
        }
      }
      //...
    }
    

    You can also use the arguments object in this way:

    function getData (/*id, parameters, callback*/) {
      var id = arguments[0], parameters, callback;
    
      if (arguments.length == 2) { // only two arguments supplied
        if (Object.prototype.toString.call(arguments[1]) == "[object Function]") {
          callback = arguments[1]; // if is a function, set as 'callback'
        } else {
          parameters = arguments[1]; // if not a function, set as 'parameters'
        }
      } else if (arguments.length == 3) { // three arguments supplied
          parameters = arguments[1];
          callback = arguments[2];
      }
      //...
    }
    

    If you are interested, give a look to this article by John Resig, about a technique to simulate method overloading on JavaScript.

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

Sidebar

Ask A Question

Stats

  • Questions 224k
  • Answers 224k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I guess your function should be fn(struct symtab* sp) instead… May 13, 2026 at 12:49 am
  • Editorial Team
    Editorial Team added an answer IB just sets up your XIB files. It's your view… May 13, 2026 at 12:49 am
  • Editorial Team
    Editorial Team added an answer undefined reference refers to a problem with the linker. The… May 13, 2026 at 12:49 am

Related Questions

I want to login to a website ( http://www.orkut.com ) through com.gargoylesoftware.htmlunit.WebClient But when
I have a view that contains a javascript function that i need to init/call
I have a lot of JavaScript/ jQuery code blocks to handle asynchronous data processing
I've been reading Diaz's book Pro JavaScript Design Patterns. Great book. I myself am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.