0

In a function (fenceBasic) taking an object as parameter and de-structuring it in order to pick only what is needed, can the (sub-)arguments be restructured as a literal object.

There is an arguments object traditionally available , but it appears that it's an undefined one in the following fenceSpread function :

const next = (obj) => {
    console.assert(obj.arg1 === 'A', obj);
    console.assert(obj.arg2 === 'B', obj);
};

const fenceBasic = ({ arg1, arg2 }) => {
    console.log('basic way');
    next({ arg1, arg2 });
};

const fenceSpread = ({ arg1, arg2 }) => {
    console.log('spread way');
    next({...arguments});
    console.debug(arguments);
};

const various_data = {
    arg1: 'A',
    arg2: 'B',
    arg3: 'C'
};

fenceBasic(various_data);
fenceSpread(various_data);

Is there an other way to achieve this or should it stay on the "fenceBasic" form ?

challet
  • 870
  • 9
  • 21
  • 2
    `=>` functions don't have an `arguments` object. – Pointy Dec 21 '19 at 15:43
  • That explains things, so it is only available in the classical `function`s then. – challet Dec 21 '19 at 15:45
  • That's correct. Of course if a `=>` function is lexically created inside a traditional function, then `arguments` refers to the object in the surrounding function (which doesn't really do you any good here). – Pointy Dec 21 '19 at 15:47
  • 1
    Changing my code to use `function`, that works as expected to spread them in the literal object. Each de-structured argument is an entry in `arguments`. – challet Dec 21 '19 at 15:49
  • next functio pass a oject means you should pass a onject like next({...arguments}); in feensespeeed – LDS Dec 22 '19 at 05:25

0 Answers0