1

I have a function that is meant to receive two parameters (both object instances of the same class):

class Person {
  constructor(name) {
    this.name = name;
  }
}

const x = new Person('Peter');
const y = new Person('Paul');
const z = new Person('Paul');

const sameName = (a, b) => a.name === b.name;

console.log(sameName(x,y));
console.log(sameName(y,z));
console.log(sameName(x,z));

I know a working way to destructure the name from each person right in the function signature is sameName({name: a},{name: b}). That feels clunky somehow.

class Person {
  constructor(name) {
    this.name = name;
  }
}

const x = new Person('Peter');
const y = new Person('Paul');
const z = new Person('Paul');

const sameName = ({name: a},{name: b}) => a === b;

console.log(sameName(x,y));
console.log(sameName(y,z));
console.log(sameName(x,z));

Q1: I'm wondering if there is a more elegant way to make use of Object destructuring here?

Q2: How would I go about a function that takes any number of Person objects (and only works with the name property of each)?

connexo
  • 53,704
  • 14
  • 91
  • 128
  • Sorry, missed that. But its not really clunky, its the syntax for this. What is a more real world reason for any umber of arguments? – Bibberty May 23 '19 at 00:04
  • What syntax are you aiming to get closer to? That looks pretty succinct to me. – Carcigenicate May 23 '19 at 00:05
  • Destructuring seems clunky here because it's unnecessary; what are you trying to do that needs it? – Herohtar May 23 '19 at 00:06
  • @Herohtar I've massively simplified the code just for the sake of demonstrating the issue. It's not exactly that I need it (in the sense of *I cannot do it any other way*); it's just that I'd like to know the most concise way to achieve that. Also it seems cleaner if all the function needs is one property of each object, to only pass that into the function body. – connexo May 23 '19 at 00:09
  • That's literally the only syntax for destructuring. If you want a "cleaner" function just make it compare `a` and `b` and pass `same(x.name, y.name)` – Herohtar May 23 '19 at 00:11
  • If that's true, that would be the answer to Q1. How about Q2? – connexo May 23 '19 at 00:12
  • For Q2 see: https://stackoverflow.com/questions/2141520/javascript-variable-number-of-arguments-to-function – Herohtar May 23 '19 at 00:12
  • @Herohtar Maybe I need to clarify Q2. I'm asking how to use that kind of destructuring on a variable number of arguments passed. – connexo May 23 '19 at 00:14
  • You can't do that as part of the variable arguments. – Herohtar May 23 '19 at 00:15

1 Answers1

3

I think it is not possible to use Object destructuring with a variable number of arguments... You can do something like this

class Person {
  constructor(name) {
    this.name = name;
  }
}

const x = new Person('Peter');
const y = new Person('Paul');
const z = new Person('Paul');

const sameName = ({ name }, ...people) =>
  people.every(person => person.name === name);

console.log(sameName(x,y));
console.log(sameName(y,z));
console.log(sameName(x,z));
Matteo Basso
  • 2,694
  • 2
  • 14
  • 21