Retaining the Root Object Name in ES6 Destructured Function Parameters
In ES6, destructuring function parameters allows for concise extraction of individual object properties. However, it raises the question of how to retain the name of the root object when only certain properties are destructured.
Passing the Root Object in ES5
In ES5, objects can be passed by reference using the inheritance metaphor. For example, a "setupParentClass5" function takes an "options" object and uses it to configure a "textEditor." A "setupChildClass5" function inherits these options by calling "setupParentClass5(options)."
Challenges with ES6 Destructuring
In ES6, using destructured parameters like "const {rows, columns}" in "setupParentClass6" only extracts the specific properties listed. This can leave the root "options" object inaccessible within "setupChildClass6."
Solution Using an Additional Argument
One solution is to add an extra argument, as shown in:
const setupChildClass6 = options => { const {minVal, maxVal} = options; rangeSlider.setup(minVal, maxVal); setupParentClass6(options); };
This approach allows "setupChildClass6" to access the complete "options" object, including both destructured and non-destructured properties.
The above is the detailed content of How to Preserve the Original Object Name When Using Destructured Function Parameters in ES6?. For more information, please follow other related articles on the PHP Chinese website!