简体   繁体   English

ES6 解构 function 参数-命名根 object

[英]ES6 destructuring function parameter - naming root object

Is there a way to retain the name of a destructured function argument?有没有办法保留解构的 function 参数的名称? Ie, the name of the root object?即根的名字object?

In ES5, I might do this (using inheritance as a metaphor to make the point):在 ES5 中,我可能会这样做(使用 inheritance 作为比喻来说明这一点):

// ES5:
var setupParentClass5 = function(options) {
    textEditor.setup(options.rows, options.columns);
};

var setupChildClass5 = function(options) {
    rangeSlider.setup(options.minVal, options.maxVal);
    setupParentClass5(options); // <= we pass the options object UP
};

I'm using the same options object to hold multiple configuration parameters.我使用相同的options object 来保存多个配置参数。 Some parameters are used by the parent class, and some are used by the subclass.有些参数是父类class用的,有些是子类用的。

Is there a way to do this with destructured function arguments in ES6?有没有办法在 ES6 中使用解构的 function arguments 来做到这一点?

// ES6:
var setupParentClass6 = ({rows, columns}) => {
    textEditor.setup(rows, columns);
};

var setupChildClass6 = ({minVal, maxVal}) => {
    rangeSlider.setup(minVal, maxVal);
    setupParentClass6( /* ??? */ );  // how to pass the root options object?
};

Or do I need to extract all of the options in setupChildClass6() so that they can be individually passed into setupParentClass6() ?或者我是否需要提取setupChildClass6()中的所有选项,以便它们可以单独传递到setupParentClass6()中?

// ugh.
var setupChildClass6b = ({minVal, maxVal, rows, columns}) => {
    rangeSlider.setup(minVal, maxVal);
    setupParentClass6({rows, columns});
};

I have the 'options' arguments on too many places myself.我自己在太多地方都有“选项”参数。 I would opt for 1 extra line of code.我会选择 1 行额外的代码。 Not worthy in this example, but a good solution when having destructuring on more lines.在这个例子中不值得,但是当在更多行上进行解构时是一个很好的解决方案。

const setupChildClass6 = options => {
    const {minVal, maxVal} = options;
    rangeSlider.setup(minVal, maxVal);
    setupParentClass6(options); 
};

You cannot do it directly in the arguments, but you can extract the values afterward:您不能直接在参数中执行此操作,但您可以在之后提取值:

function myFun(allVals) {
    const { val1, val2, val3 } = allVals;
    console.log("easy access to individual entries:", val2);
    console.log("easy access to all entries:", allVals);
}

You cannot use destructuring and simple named positional argument for the same parameter at the same time.您不能同时对同一个参数使用解构和简单命名位置参数。 What you can do:你可以做什么:

  1. Use destructuring for setupParentClass6 function, but old ES6 approach for setupChildClass6 (I think this is the best choice, just make name shorter):使用解构为setupParentClass6功能,但老ES6方法为setupChildClass6 (我认为这是最好的选择,只是做短的名字):

     var setupChildClass6 = (o) => { rangeSlider.setup(o.minVal, o.maxVal); setupParentClass6(o); };
  2. Use old arguments object.使用旧的arguments对象。 But arguments can slow down a function (V8 particular), so I think it's a bad approach:但是arguments会减慢一个函数的速度(特别是 V8),所以我认为这是一个糟糕的方法:

     var setupChildClass6 = ({minVal, maxVal}) => { rangeSlider.setup(minVal, maxVal); setupParentClass6(arguments[0]); };
  3. ES7 has proposal for rest/spread properties (if you don't need minVal and maxVal in setupParentCalss6 function): ES7 有关于rest/spread 属性的建议(如果你在setupParentCalss6函数中不需要minValmaxVal ):

     var setupChildClass6b = ({minVal, maxVal, ...rest}) => { rangeSlider.setup(minVal, maxVal); setupParentClass6(rest); };

    Unfortunately it's not ES6.不幸的是,它不是 ES6。

Personally I'd rather destructure within the function body, but for completeness I wanted to show that it's technically possible to access both the "root" object and destructured properties via parameters.就我个人而言,我宁愿在 function 体内进行解构,但为了完整起见,我想表明在技术上可以通过参数访问“根”object 和解构的属性。

To do we take advantage of the fact that parameters have access to preceding parameters in their default values.为此,我们利用参数可以访问其默认值中的前面参数这一事实。 That allows you to define a second parameter that destructures the first one:这允许您定义第二个参数来解构第一个参数:

var setupChildClass6 = (options, {minVal, maxVal}=options) => {
    rangeSlider.setup(minVal, maxVal);
    setupParentClass6(options);
};

So this function takes two arguments and defaults to using the first value of the first argument for the second argument.所以这个 function 需要两个 arguments 并且默认使用第一个参数的第一个值作为第二个参数。

Of course you'd have to take great care that it's communicated properly what the purpose of this is.当然,您必须非常小心,以正确传达其目的。

This is the best I could come up with.这是我能想到的最好的方法。 Not great, but far better (IMO) than doing the destructing on another line.不是很好,但比在另一条线上进行破坏要好得多(IMO)。 It gives you a heads-up in a way too that you are going to provide options first and foremost, and also that you are destructuring it in the function...它也以某种方式提醒您,您将首先提供options ,并且您正在函数中对其进行解构...

var setupParentClass6 = options => (({rows, columns}) => {
    textEditor.setup(rows, columns);
})(options);

var setupChildClass6 = options => (({minVal, maxVal}) => {
    rangeSlider.setup(minVal, maxVal);
    setupParentClass6(options);
})(options);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM