0

为什么tom的showName()原型和jane的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]
4

0 回答 0