javascript es6: use case for destructuring rest parameter

André Marcondes Teixeira

I just saw a code snippet in MDN about destructuring rest parameters like so:

function f(...[a, b, c]) {
  return a + b + c;
}

f(1)          // NaN (b and c are undefined)
f(1, 2, 3)    // 6
f(1, 2, 3, 4) // 6 (the fourth parameter is not destructured)

the code snippet is in this page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters

Although the common use case for rest parameters is very clear to me (function foo(...params){/*code*/}) I could not think about a real world use case to use rest parameters like the way presented in that code snippet. Instead, I think that in that case, I should just use a common function definition:

function f(a, b, c) {
  return a + b + c;
}

f(1)          // NaN (b and c are undefined)
f(1, 2, 3)    // 6
f(1, 2, 3, 4) // 6 (the fourth parameter is not defined)
André Marcondes Teixeira

let's say that we have a function that returns an object such like that:

function getCustomer(id) {
    return fetch(`http://myapi.com/customer/${id}`);
}

and let's say I have a response like that:

{
    "customer": {
        "id": 1234,
        "name": "John Doe",
        "latestBadges": [
            "Platinum Customer",
            "100 Buys",
            "Reviewer"
        ]
    }
}

In a more traditional approach I could write a function to show the latest 3 badges like so:

function showLatestBadges(a, b, c) {
    console.log(a, b, c);
}

and to use that function, I would need to to:

getCustomer(1234).then((customer) => {
    showLatestBadges(
        customer.latestBadges[0],
        customer.latestBadges[1],
        customer.latestBadges[2]
    );
});

With this new spread operator, I could do this instead:

getCustomer(1234).then((customer) => {
    showLatestBadges(...customer.latestBadges);
});

So, using the spread operator in the function definition may look like it's a little useless. But, in fact, it CAN be useful in a VERY specific situation:

Let's say we have a legacy system, and let's say that the call to the showLatestBadges function is being made in hundreds of places without using the spread operator, just like the old days. Let's also assume that we are using a linting tool that prevents unused variables, and let's also assume that we are running a build process that do cares about the linting results, and if the linting says that something is not right, the build fails. Let's ALSO ASSUME that for some weird business rule, we now have to show only the first and third badges. Now, assuming this function call being made in hundreds of places in the legacy system, and we do not have much time available to deliver the implementation of this new business rule, we do not have time to refactor the code for ALL those hundreds of calls. So, we will now change the function as so:

function showLatestBadges(a, b, c) {
    console.log(a, c);
}

But now we have a problem: the build fails because of the unused b variable, and we have to deliver this change for YESTERDAY!!! We have no time to refactor all the hundreds of calls to this function, and we cannot just do a simple find and replace in all the spots, because we have such a messy code, and there are evals all over the place, and unpredictable behavior can happen.

So, one solution is: change the function signature using the spread operator, so the build succeeds, and create a task on the board to do the refactoring. So, we can change the function as so:

function showLatestBadges(...[a,,c]) {
    console.log(a, c);
}

Ok, I know this is a VERY specific situation and that this is very unlike to happen, but, who knows? ¯\_(ツ)_/¯

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

javascript es6: import destructuring not working

How to use array destructuring and assign to an object property in JavaScript ES6

Destructuring a spreaded javascript parameter

ES6 destructuring function parameter - naming root object

unclear on ES6 destructuring function parameter defaults

Destructuring a function parameter object and ...rest

Object Destructuring with Rest Parameter, on Array

How to use destructuring assignment to define enumerations in ES6?

What can I use as placeholders in es6 array destructuring?

How to use deep destructuring on imports in ES6 syntax?

Simple Destructuring Program - ES6 (JavaScript) throwing error

Simplify javascript object creation using ES6 destructuring

What is the use case for var in ES6?

Javascript es6 - is it reassigning or a parameter?

ES6 destructuring object, default value on assignment when not used as a function parameter?

Correct way to use destructuring in JavaScript

ES6 destructuring preprocessing

ES6 Destructuring assignment with `this`

Promises with ES6 destructuring?

Vanilla Javascript use of bind() with REST parameter

ES6: What happens if the rest parameter is an array?

Use of if-else in JavaScript ES6

How to use ES6 destructuring assignment to assign one object to another

What's the difference between object destructuring and normal object assignment in Javascript ES6?

Javascript ES6 Destructuring - Identifier 'location' has already been declared

Why does ES6 Object Destructuring not work with native javascript prototypes?

Why destructuring works differently than in classic assignation in Javascript (ES6)?

Use default parameter when using the spread syntax in ES6?

JS string destructuring: rest parameter returning inconsistent data