原型链与原型链继承

1. 原型

对象:__proto__

函数对象: __proto__   prototype

原型链结构

 查看一个空对象, __proto__ . __proto__ . __proto__ .  每个__proto__ 指向创建它的__proto__  最终指向Object的原型prototype (Object是一个函数对象)

查找不到返回null

2. 函数继承

//原型链继承
function Father(name, color) {
  this.name = name;
  this.color = color;
}
Father.prototype.running = function () {
  return `名字:${this.name}`;
};
function Child(age, nos) {
  this.age = age;
  this.nos = nos;
}
Child.prototype = new Father();
Child.prototype.eating = function () {
  return `eating:${this.name}`;
};
let Childone = new Child("age", "nos");
console.log("原型链继承", Childone);
//借用构造函数继承
function Child(age, nos) {
  Father.call(this, name, color);
  this.age = age;
  this.nos = nos;
}
//寄生式继承
function foo(o) {
  let Fn = function () {};
  Fn.prototype = o;
  return new Fn();
}
//寄生组合式继承
function inheritPrototype(son, dad) {
  son.prototype = foo(dad.prototype);
  son.defineproperty(son.prototype, "construction", {
    enumberable: false,
    configurable: true,
    writerable: false,
    value: dad,
  });
}

 

posted @ 2023-05-14 12:27  zhangzhebokeyuan  阅读(11)  评论(0编辑  收藏  举报