harwind

harwind

First have a look at the code:

function mainfunc(func, par3, par2){
    window[func](par3, par2);
}

function calledfunc(par3, par2){
    // Do stuff here
}

mainfunc('calledfunc', 'hello', 'bye');

I’m looking for a solution to this. In JavaScript, I know how to call a dynamic, arbitrary function while passing specific parameters

function mainfunc(func){
    if(arguments.length == 5)
        window[func](arguments[1], arguments[2]);
    else if(arguments.length == 3)
        window[func](arguments[1], arguments[2], arguments[3]);
    else if(arguments.length == 5)
        window[func](arguments[1], arguments[2], arguments[3], arguments[4]);
}

function calledfunc1(par1, par2){
    // Do stuff here
}

function calledfunc2(par1, par2, par3){
    // Do stuff here
}

mainfunc('calledfunc1', 'hello', 'bye');
mainfunc('calledfunc2', 'hello', 'bye', 'goodbye');

I understand how to pass optional, unlimited parameters using the arguments collection inside mainfunc, but I can’t figure out how to send an arbitrary number of parameters to mainfunc to be sent to calledfunc dynamically like this one; how can I accomplish something similar, but with any number of optional arguments (without using that ugly if-else)?

/js

Popular Frontend topics Top

AstonJ
Inspired by @dbernheisel’s post here, which CSS framework do you use and why?
New
bot
Vue.js v2.6.11 has been released. Link: Release v2.6.11 · vuejs/vue · GitHub
New
finner
Some resources for Spring Home of Spring Spring Initializr Head First Spring with Reactor
New
First poster: bot
A new Svelte blog post/announcement has been posted! Get the full details here: What's new in Svelte: January 2021
New
First poster: bot
Dream is an easy-to-use, boilerplate-free Web framework, whose entire API fits on this page! It supports TLS, WebSockets, and GraphQL. H...
New
mafinar
I (re-)realized I had the book “Modern CSS with Tailwind” bought, never read a page of it until today. With me being a fast reader and th...
New
First poster: bot
A new Angular blog post/announcement has been posted! Get the full details here: https://blog.angular.io/build-more-accessible-angular-...
New
First poster: bot
Tailwind v2.2.10, v2.2.9 and v2.2.8 has been released. Link: Release v2.2.10 · tailwindlabs/tailwindcss · GitHub Link: Release v2.2.9...
New
PragmaticBookshelf
This new edition of the book covers Tailwind 3.0, which changes the way Tailwind generates its CSS. Tailwind 3.0 has a large number of ne...
New
AlessandroDsgroup
Hi to everyone, we are experiencing a 401 error related to the connection of the websocket (in reference to our web app); we are unable ...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
New
Exadra37
I am a Linux user since 2012, more or less, and I always use Ubuntu on my computers, and my last 2 laptops have been used Thinkpads, wher...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New