【问题标题】:javascript: get un-destructed parameter in functionjavascript:在函数中获取未破坏的参数
【发布时间】:2018-06-26 11:58:37
【问题描述】:

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

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);
}

【问题讨论】:

标签: javascript ecmascript-6 destructuring


【解决方案1】:

你可以在foo 中有一个参数并在其中进行解构。然后,您将使用urloptionsheaderbodyauth 做一些事情,最后调用bar 就像bar({ ...args, auth })spreading args 和添加@9876543好吧:

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;
}

【讨论】:

    猜你喜欢
    • 2017-02-10
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 2016-08-27
    • 2018-06-30
    • 2012-11-26
    • 1970-01-01
    相关资源
    最近更新 更多