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)?