harwind

harwind

In javascript, how do you call a function?

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

Most Liked

Hallski

Hallski

If I understand you correctly you can achieve this through using the rest parameters syntax to collect the arguments into an array and the spread-syntax to expand the array when calling the function.

Popular Frontend topics Top

PragmaticBookshelf
Database-driven sites bring complexity you might not need, but building a site by hand is too much work. Hugo is a static site generator ...
New
AstonJ
DHH might like you to believe they are a thing of the past: https://twitter.com/dhh/status/1275902566719610880 I actually went down thi...
New
AstonJ
They now have a beginners course on NodeJS, following their beginners course on Javascript. I’m hoping they will do one o...
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 the deal with SvelteKit?
New
AstonJ
Which WebAssembly language/s interest you? I just discovered AssemblyScript (portal | thread) which looks interesting! Here are some of...
New
AstonJ
Yew looks really interesting!! What is Yew? Yew is a modern Rust framework for creating multi-threaded front-end web apps with WebAssem...
New
mafinar
Just like I did one on Tailwind some time ago, I am going to play with AlpineJS the next few days. It’s a nice little JS framework that ...
New
First poster: bot
Tailwind v3.0.12 has been released. Link: Release v3.0.12 · tailwindlabs/tailwindcss · GitHub
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

malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
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
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New