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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:07:00+00:00 2026-05-23T07:07:00+00:00

in my project, I register different functions (having different number of arguments) as listeners

  • 0

in my project, I register different functions (having different number of arguments) as listeners to a number of events. When the event takes place, I need to fire the associated function. I receive the parameters to be passed to listener method in the form of an array whereas the listener function expect each separate argument. So, I am doing it like this but I do not like the approach and would like to know if there is an elegant way of doing it,

function callListenerWithArgs(func, args){
        switch(args.length){
            case 1:
                func(args[0]);
                break;
            case 2:
                func(args[0], args[1]);
                break;
            case 3:
                func(args[0], args[1], args[2]);
                break;
            case 4:
                func(args[0], args[1], args[2], args[3]);
                break;
            default:
                func();
        }
    }
  • 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-23T07:07:00+00:00Added an answer on May 23, 2026 at 7:07 am

    Use .apply

    func.apply(null, args)
    

    If you need to bind to a specific scope, you can pass another argument in to use as this inside the function:

    func.apply(scope, args);
    

    Also, a nuance of JavaScript is that you can call functions with undefined values. So making a small tweak to your existing code will work in 95% of all cases (this isn’t suggested as a solution, just pointing it out):

    // will handle any number of args up to 7
    function callListenerWithArgs(func, args){
        func(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
    }
    

    If your func is defined as:

    function foo(a, b, c){
    }
    

    you get a, b, c passed in, along with some more undefined values that get ignored. As I said above, this works in 95% of cases. It doesn’t work if you ever check arguments.length in the called function since it will always be the same, regardless of the number of parameters the function defines.

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

Sidebar

Related Questions

The project I'm working on has a need to represent many different hardware registers
In my project I register many ISerializers implementations with the assembly scanner. FWIW this
I'm working on a project the requires me to register a domain for a
When i setup an MVC 3 project, there is an implementation of user register
I'm trying to use .NET HttpListener in a C# project. When I register my
Project layout: /project_a /shared /project_b /shared /shared project_a and project_b both need to contain
My project in some part need GPU acceleration to make a fast translation, but
When we create a directshow filter and register it, static variables/functions are shared in
Im trying to register an event listener for a successful login, so that I
I have standart MVC web project that register routes in global.asax. My MVC project

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.