0

Why is there a difference between prototype of tom's showName() and prototype of jane's showName()?

class User {

  constructor(_name, _age) {
    this.name = _name;
    this.age = _age;
    this.showAge = function() {
      console.log(this.age);
    }
  }
  
  showName = function() {
    console.log(this.name);
  }
  
}

const tom = new User('Tom', 20);
tom; // User2 {name: "Tom", age: 25}
name: 'Tom'
age: 25
showAge: f()[prototype]
showName: f()

class User2 {

  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  
  showName() {
    console.log(this.name);
  }
  
}

const jane = new User2('Jane', 25);
jane; // User3 {name: "Jane", age: 25}
name: 'Jane'
age: 25
showAge: f()
showName: f()[prototype]
Andy
  • 53,323
  • 11
  • 64
  • 89
Jsmile
  • 21
  • 3

0 Answers0