javascript: 获取函数中未析构的参数

javascript: get un-destructed parameter in function

我需要在函数中获取未破坏的参数。什么是最好的方法?

const foo = ({url, options, header, body, auth = 1}) => {
    //...do things with the parameters...
    let params = {url, options, header, body, auth}; // Is there an easy way?
    bar(params);
}

您可以在 foo 中有一个参数并在其中进行解构。然后,你会用 urloptionsheaderbodyauth 做一些事情,最后调用 bar 比如 bar({ ...args, auth }), spreading args 并添加 auth 以及:

const bar = (baz) => {
  console.log(baz);
};

const foo = (args) => {
  const { url, options, header, body, auth = 1 } = args;

  // Do stuff with url, options, header, body and auth...

  bar({ ...args, auth });
};

foo({ url: 'www.google.com', options: {}, header: {}, body: 'Hi there!' });
foo({ url: 'www.google.com', options: {}, header: {}, body: 'Hi there!', auth: false });
.as-console-wrapper {
  max-height: 100vh !important;
}