My code logs 2 in freeCodeCamp console, but logs undefined in my local consol

Tell us what’s happening:
this runs normally in the local console code when I don’t use the class syntax and use only constructor function , But when I use the class syntax it logs Undefined , this happens only in the local console, I tried two local console .
but in freecodecamp console works perfectly.

is there difference between es6 class syntax and other constructor function declaration ?

  **Your code so far**

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

User.prototype = {
  constructor:User,
  numLegs: 2,
};
let user = new User("Yahia");
console.log(user.numLegs);


  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Challenge: Change the Prototype to a New Object

Link to the challenge:

I’m no expert here …

Yeah, I’m not sure if classes are supposed to be used like that. I know this will work:

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

User.prototype.numLegs = 2

let user = new User("Yahia");

console.log(user.numLegs);

I don’t know if that is a good practice - I really don’t know.

I don’t know about trying to overwrite the entire prototype like that. That may work with constructor function, I don’t know about classes. In my work I don’t use object constructors at all. I use a lot of classes, but I don’t have the need to manipulate them like that.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.