Allow for passing parents into the constructor. Also, add some spaces to make code consistent. Also, avoid mutating parents array, just for the sake of it.
class Human { constructor (firstName = '', lastName = '', parents = []) { this.firstName = firstName; this.lastName = lastName; this.parents = parents; // modern family. who takes parental care? } filterMyFamily(humans) { return humans.filter(human => human.lastName === this.lastName) } hasParent(p) { return this.parents.some(parent => parent === p); } hasChild(c) { return c.hasParent(this); } addParent(p) { if (!this.hasParent(p)) this.parents = [ ...this.parents, p ]; return this; } }
- class Human {
constructor (firstName = '', lastName = '') {- constructor (firstName = '', lastName = '', parents = []) {
- this.firstName = firstName;
- this.lastName = lastName;
this.parents = []; // modern family. who takes parental care?- this.parents = parents; // modern family. who takes parental care?
- }
- filterMyFamily(humans) {
- return humans.filter(human => human.lastName === this.lastName)
- }
addParent(p){if (!this.hasParent(p)) this.parents.push(p);return this;- hasParent(p) {
- return this.parents.some(parent => parent === p);
- }
hasChild(c){- hasChild(c) {
- return c.hasParent(this);
- }
hasParent(p){return this.parents.some(parent => parent === p);- addParent(p) {
- if (!this.hasParent(p)) this.parents = [ ...this.parents, p ];
- return this;
- }
- }
Simplify hasParent
method with Array#some. Read more at https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some.
class Human { constructor (firstName = '', lastName = '') { this.firstName = firstName; this.lastName = lastName; this.parents = []; // modern family. who takes parental care? } filterMyFamily(humans) { return humans.filter(human => human.lastName === this.lastName) } addParent(p){ if (!this.hasParent(p)) this.parents.push(p); return this; } hasChild(c){ return c.hasParent(this); } hasParent(p){ return this.parents.some(parent => parent === p); } }
- class Human {
- constructor (firstName = '', lastName = '') {
- this.firstName = firstName;
- this.lastName = lastName;
- this.parents = []; // modern family. who takes parental care?
- }
- filterMyFamily(humans) {
- return humans.filter(human => human.lastName === this.lastName)
- }
- addParent(p){
- if (!this.hasParent(p)) this.parents.push(p);
- return this;
- }
- hasChild(c){
- return c.hasParent(this);
- }
- hasParent(p){
for (var i=0; i<this.parents.length; i++){if (this.parents[i] === p) return true;}return false;- return this.parents.some(parent => parent === p);
- }
- }
Use ES6 default arguments and strict equality operator.
class Human { constructor (firstName = '', lastName = '') { this.firstName = firstName; this.lastName = lastName; } filterMyFamily(humans) { return humans.filter(human => human.lastName === this.lastName) } }
- class Human {
constructor (firstName, lastName) {this.firstName = firstName || ""this.lastName = lastName || ""- constructor (firstName = '', lastName = '') {
- this.firstName = firstName;
- this.lastName = lastName;
- }
- filterMyFamily(humans) {
return humans.filter(human => (human.lastName == this.lastName))- return humans.filter(human => human.lastName === this.lastName)
- }
- }