Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: Cannot read properties of undefined (reading 'a')

I was trying classes and the this keyword in Javascript,and I wrote this code:

class p{
  constructor(p,a){
      p = p.p;
      a = p.a;
  };
};
const c = new p(p="1st aspect",a="2nd aspect");
console.log(this.c);

And I got this Error:

Cannot read properties of undefined (reading 'a')

I don’t know whether this is compatible with classes or whether I passed in the wrong arguments,can someone help me there?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The syntax for creating a class is wrong. You don’t need to pass the parameter names. Just use

const c = new p("1st aspect","2nd aspect");

And it is not good practice to use lowercased frist letter as class name. Use P instead of p. And this.c is wrong. Just print

console.log(c);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading