Call javascript function with arguments using arguments received

My problem is best illustrated by the small code sample available on JsFiddle :

function a() {
    alert("a: " + arguments.length);
    b(arguments);
}

function b() {
    alert("b: " + arguments.length);
}

a(33,445,67);

I have a function acalled with a variable number of arguments, and I would like to call bwith these arguments. Unfortunately, when I run the code above, it displays 3 and 1, respectively, and not 3 and 3.

How can I call bwith all arguments received in a?

+4
source share
1 answer

arguments - , , , , , b(), , .
apply(),

function a() {
    alert("a: " + arguments.length);
    b.apply(undefined, arguments);
}

function b() {
    alert("b: " + arguments.length);
}

a(33,445,67);

FIDDLE

+6

Source: https://habr.com/ru/post/1533692/


All Articles