Mylofi

Javascript 之原型链

1 min read#Javascript
 
function SuperType() {
  this.property = true;
}
SuperType.prototype.getSuperValue = function() {
  return this.property;
}
function SubType() {
  this.subproperty = false;
}
//  修改 SubType 的原型,重新指向 SuperType 
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function() {
  return this.subproperty;
}
let instance = new SubType();
console.log(instance.getSuperValue()) // true
console.log(instance.getSubValue()) // false
 

评论功能未启用:在 lib/site.ts 中填写 Giscus 配置即可 (配置向导见 giscus.app)。