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

PragmaticBookshelf
Upgrade your skill set, succeed at work, and avoid the many headaches that come with modern front-end development. Simplify your codebase...
New
AstonJ
Sprinkles of JS, or full frontend frameworks? What do you use and why?
New
AstonJ
Here’s a pretty good list of them: https://github.com/jashkenas/coffeescript/wiki/List-of-languages-that-compile-to-JS What’s your favou...
New
mafinar
Wanted to try out Surface for awhile now and yesterday finally had the time for it, the developer experience is amazing and I decided to ...
New
First poster: bot
ThinScript ThinScript is an experimental programming language that compiles to JavaScript, WebAssembly, and C. It’s meant to be a thin la...
New
First poster: bot
Preact 10.5.13 has been released. Link: Release 10.5.13 · preactjs/preact · GitHub
New
First poster: bot
A new Svelte blog post/announcement has been posted! Get the full details here: What's new in Svelte: May 2021
New
ClaudiaGiv
I have a sveltekit project that is using GoTrue library for authentication. In development mode (npm run dev) everything works but when I...
New
JesseSkinner
I’ve developed a video course called The Joy of Svelte, where I go in depth on Svelte features, especially different ways to manage state...
New
pillaiindu
I mean, when you render all the HTML at the server side and the data is sent through HTTP requests, except only if some tiny things are d...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1016 16827 371
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
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
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New