如何访问原型方法

How to access prototype method

本文关键字:原型 方法 访问 何访问      更新时间:2023-09-26
function Person1(){
  this.name='Person1 Name';
  this.Age='Person1 Age';
  this.Sex='Person1 Sex';
}
Person1.prototype.getInfo=function(){
  return this.name;
}

var Person2=Object.create(Person1.prototype);
console.log(Person2.getInfo());

我想通过继承输出为 Person1 名称,有什么方法可以实现它。在这里我想知道Person2包含什么?有什么不同方式?

function Person1(){
  this.name='Person1 Name';
  this.Age='Person1 Age';
  this.Sex='Person1 Sex';
}
Person1.prototype.getInfo=function(){
  return this.name;
}

var Person2= new Person1();
console.log(Person2.getInfo());