
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

Why or when should one choose Tailwind over Bootstrap? :slight_smile:
New

What does svelte offer that React or Vue doesn’t?
New

What is the best course of front end (live webinars or recorded)? So I already have basic understanding of HTML CSS JS and React but I wa...
New

How to make a website like webnovel and wattpadd where subscribers and logged in user post stories like their own Novel in a website and ...
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

Hi this is Palak Sharma
I am new here and I found this community while researching about JavaScript over the internet.
Well I have comp...
New

I have a 1D array of numbers and need help splitting them into groups using a jagged array so that I can perform a series of computations...
New

Background
I have a fresh umbrella app and I am trying to create a Phoenix app inside it. However, even though I can create the Phoenix a...
New

I’m currently working on a front-end development project and I’m facing an issue with aligning items using CSS Flexbox. I want to horizon...
New

hello , i should code a cluster like image bellow we have no challenge in coding backend but in front need some clue to do this
its a dy...
New
Other popular topics

What chair do you have while working… and why?
Is there a ‘best’ type of chair or working position for developers?
New

I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New

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

Not sure if following fits exactly this thread, or if we should have a hobby thread…
For many years I’m designing and building model air...
New

Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New

Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New

Biggest jackpot ever apparently! :upside_down_face:
I don’t (usually) gamble/play the lottery, but working on a program to predict the...
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

The File System Access API with Origin Private File System.
WebKit supports new API that makes it possible for web apps to create, open,...
New

If you want a quick and easy way to block any website on your Mac using Little Snitch simply…
File > New Rule:
And select Deny, O...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /ash
- /vscode
- /opensuse
- /centos
- /php
- /deepseek
- /zig
- /scala
- /html
- /debian
- /nixos
- /lisp
- /agda
- /textmate
- /sublime-text
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /manjaro
- /django
- /spring
- /diversity
- /lua
- /nodejs
- /c
- /slackware
- /julia
- /neovim