Search code examples
javascriptconstructorprototypedestructuring

destructuring arguments in a group of constructors in JS


How can I 1) pass the prototype props from parent-constructor to child-constructors, 2) using destructure to organice the parameters of all my constructors, without causing a Type error?

this is the code I want to fix:

function Participant ({gender, age, tastes, education}){
  this.gender = gender;
  this.age = age; 
  this.tastes = tastes || [];
  this.education = education || 0;
}

function Payer(gender, age, tastes, education, income, job){
  this.base = Participant;
  this.base(gender, age, tastes, education); 
  this.income = income;
  this.job = job || 'merchant';
}
Payer.prototype = new Participant;

function NotPayer({gender, age, tastes, role}){
  this.role = role || 'kid';
  this.base = Participant;
  this.job = 'Not Valid';
  this.base(gender, age, tastes);
}
NotPayer.prototype = new Payer;

const kid1 = new NotPayer({role: 'nephew', age: 6, gender: 'male', tastes: ['ps4', 'golf']});
kid1.role;// 'nephew'
const kid2 = new Participant({gender: 'female'});
kid2.gender; // Type Error: Cannot destructure property 'gender' of 'undefined'

What I tried to fix it is this:


function Participant ({gender, age, tastes, education}){
  this.gender = gender;
  this.age = age; 
  this.tastes = tastes || [];
  this.education = education || 0;
}

function Payer(gender, age, tastes, education, income, job){
  this.base = Participant;
  this.base(gender, age, tastes, education);
  this.income = income;
  this.job = job || 'merchant';
}
//Payer.prototype = new Participant;

function NotPayer({gender, age, tastes, role}){
  this.role = role || 'kid';
  this.base = Participant;
  this.job = 'Not Valid';
  this.base(gender, age, tastes);
}
//NotPayer.prototype = new Payer;

const kid1 = new NotPayer({role: 'nephew', age: 6, gender: 'male', tastes: ['ps4', 'golf']});
console.log(kid1.role); // nephew
const kid2 = new Participant({gender: 'female'});
console.log(kid2.gender); // female

It works only because I'm commenting the expression to assign the parent-constructor to the child-constructors (as a consequence, for example, kid1.gender becomes undefined)

To note that The code manage to use both, Parent prototype inheritance and parameter destructuring, only in the following case (when using destructuring just in the last constructor child):

function Participant (gender, age, tastes, education){
  this.gender = gender;
  this.age = age; 
  this.tastes = tastes || [];
  this.education = education || 0;
}
function NotPayer({gender, age, tastes, role}){
  this.role = role || 'kid';
  this.base = Participant;
  this.job = 'Not Valid';
  this.base(gender, age, tastes);//check education
}
NotPayer.prototype = new Participant;

const kid1 = new NotPayer({role: 'nephew', age: 6, gender: 'male', tastes: ['ps4', 'golf']});
console.log(kid1.gender); // male

console.log(NotPayer.prototype); // Participant{...}

Thanks!! and sorry about the extent of this question.


Solution

  • Try this

    class Participant {
      constructor(gender, age, tastes, education) {
        this.gender = gender;
        this.age = age;
        this.tastes = tastes || [];
        this.education = education || 0;
      }
    }
    
    class NotPayer extends Participant {
      constructor({gender, age, tastes, role}) {
        super(gender, age, tastes)
        this.role = role || 'kid';
        this.job = 'Not Valid';
      }
    }
    
    const kid1 = new NotPayer({role: 'nephew', age: 6, gender: 'male', tastes: ['ps4', 'golf']});
    console.log(kid1.gender)
    
    console.log(NotPayer.prototype)