
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)?
Most Liked

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.
2
Popular Frontend topics

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

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

They now have a beginners course on NodeJS, following their beginners course on Javascript.
I’m hoping they will do one o...
New

Some resources for Spring
Home of Spring
Spring Initializr
Head First Spring with Reactor
New

A new Svelte blog post/announcement has been posted!
Get the full details here: What's the deal with SvelteKit?
New

Which WebAssembly language/s interest you?
I just discovered AssemblyScript (portal | thread) which looks interesting!
Here are some of...
New

Yew looks really interesting!!
What is Yew?
Yew is a modern Rust framework for creating multi-threaded front-end web apps with WebAssem...
New

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

Tailwind v3.0.12 has been released.
Link: Release v3.0.12 · tailwindlabs/tailwindcss · GitHub
New

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

Any thoughts on Svelte?
Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New

No chair. I have a standing desk.
This post was split into a dedicated thread from our thread about chairs :slight_smile:
New

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

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

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

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

Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New

Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New

Will Swifties’ war on AI fakes spark a deepfake porn reckoning?
New

This is cool!
DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON
We just witnessed something incredible: the largest open-s...
New
Latest in Frontend
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /zig
- /html
- /scala
- /debian
- /nixos
- /lisp
- /agda
- /textmate
- /sublime-text
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /manjaro
- /spring
- /django
- /diversity
- /nodejs
- /lua
- /slackware
- /c
- /julia
- /neovim