Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I pass multiple parameters in javascript function

Tags:

javascript

function ceshi(test,lyj,param1,parm2){
    console.log(arguments)
}
var params = [2,3];
ceshi(eval('2,3'));

if params is not uncertain, how can I pass params through a method.I want to realize below result.

the function has fixed params,but I have more than one function like

ceshi(1,2,123)
ceshi(1,3123)
ceshi('xxx','ssssss','12313',12)
like image 915
NC LI Avatar asked Feb 19 '26 05:02

NC LI


2 Answers

You can use spread operator in ECMAScript 6 like this:

function ceshi(...params) {
  console.log(params[0]);
  console.log(params[1]);
  console.log(params[2]);
}

Or use the "arguments" variable within a function like this:

function ceshi() {
  console.log(arguments[0]);
  console.log(arguments[1]);
  console.log(arguments[2]);
}

To understand further deeper, I will highly recommend you to read this material.

like image 150
AJ H Avatar answered Feb 21 '26 19:02

AJ H


To call a function, by passing arguments from an array

  1. Either use spread operator ceshi(...params)
  2. Use apply function to invoke it ceshi.apply(<Context>, params)
like image 45
Koushik Chatterjee Avatar answered Feb 21 '26 17:02

Koushik Chatterjee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!