Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

javascript - Call function with array of arguments

Can I call a function with array of arguments in a convenient way in JavaScript?

Example:

var fn = function() {
    console.log(arguments);
}

var args = [1,2,3];

fn(args);

I need arguments to be [1,2,3], just like my array.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since the introduction of ES6, you can sue the spread syntax in the function call:

const args = [1,2,3];

fn(...args);

function fn() {
  console.log(arguments);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...