当前位置:网站首页>Several ways to implement inheritance in js
Several ways to implement inheritance in js
2022-08-09 00:06:00 【Biao Biao_】
First important point
example aboutPerson和Student的设计,不符合面向对象的设计原则,I found out after re-reading the article I wrote,I'll redo these when I have time.But it does not affect the understanding of the core content
Continuing the learning of prototypes and prototype chains,record inheritance!
原型链继承
重点:Make the prototype object of the subclass equal to the instance of the superclass.
特点:实例可继承的属性有:父类构造函数属性,Properties of the parent class prototype object.
缺点:1、新实例无法向父类构造函数传参.
2、继承单一.
3、所有新实例都会共享父类实例的属性.(原型上的属性是共享的,一个实例修改了原型属性,另一个实例的原型属性也会被修改)
function Person(skinColor = 'yellow', gender = 1) {
this.skinColor = skinColor;
this.gender = gender;
this.eat = function () {
console.log('eating!');
}
}
function Student(name = 'bob', ability = ['code', 'dance']) {
this.name = name;
this.ability = ability;
}
Student.prototype = new Person();
Student.prototype.constructor = Student;
var bob = new Student();
构造函数继承
重点:用.call()和.apply()将父类构造函数引入子类函数(在子类函数中做了父类函数的自执行(复制))
特点:1、只继承了父类构造函数的属性,没有继承父类原型的属性.
2、解决了原型链继承缺点1、2、3.
3、可以继承多个构造函数属性(call多个).
4、在子实例中可向父实例传参.
缺点:1、只能继承父类构造函数的属性.
2、无法实现构造函数的复用.(每次用每次都要重新调用)
3、每个新实例都有父类构造函数的副本,臃肿.
function Person(skinColor = 'yellow', gender = 1) {
this.skinColor = skinColor;
this.gender = gender;
this.eat = function () {
console.log('eating!');
}
}
function Student(name = 'bob', ability = ['code', 'dance']) {
this.name = name;
this.ability = ability;
Person.call(this);
}
var bob = new Student();
组合继承(Combining prototype chains and constructor inheritance)
重点:Prototype chain inheritance and constructor inheritance are combined
特点:The advantages of constructor inheritance and prototype chain inheritance are preserved,It is the most common inheritance method
缺点:执行了两次Person,属性重复了.
function Person(skinColor = 'yellow', gender = 1) {
this.skinColor = skinColor;
this.gender = gender;
this.eat = function () {
console.log('eating!');
}
}
function Student(name = 'bob', ability = ['code', 'dance']) {
this.name = name;
this.ability = ability;
Person.call(this);
}
Student.prototype = new Person();
Student.prototype.constructor = Student;
var bob = new Student();
寄生组合继承
重点:定义一个新的变量,The prototype object of the parent class is indirectly given to the child class,避免了newAn operation on an instance of a parent class
特点:A more perfect inheritance scheme,Optimized calling twice in composite inheritancePerson实例的操作
function Person(skinColor = 'yellow', gender = 1) {
this.skinColor = skinColor;
this.gender = gender;
this.eat = function () {
console.log('eating!');
}
}
function Student(name = 'bob', ability = ['code', 'dance']) {
this.name = name;
this.ability = ability;
Person.call(this);
}
var prototype = Object.create(Person.prototype);
prototype.constructor = Student;
Student.prototype = prototype;
var bob = new Student();
在es6There is a more friendly way to inheritance in :extends
对于extends的解释,需要先了解es6的class.es6真的是jsA major version of object orientation!
// Created as an anonymous class
let Person = class {
constructor(skinColor = 'yellow', gender = 1) {
this.skinColor = skinColor;
this.gender = gender;
}
eat() {
console.log('eating!');
}
}
let Student = class extends Person {
constructor(name = 'bob', ability = ['code', 'dance']) {
// super关键字用于访问和调用一个对象的父对象上的函数
super();
this.name = name;
this.ability = ability;
}
}
let bob = new Student();
边栏推荐
猜你喜欢
随机推荐
禅道数据库异机访问,远程连接,navicat连接
关于字符串根据字典序排序的方法
HCIP2--RIP实验
移动web开发-布局篇
记一次“粗暴”的Flash模拟EEPROM法(用的STM32F030C6芯片,没找到模拟EEPROM库函数)
如何使用Rancher部署发布自己的web应用
穿越派·派盘(WebDAV)解决OmniFocus同步问题
MVC与MVP的区别
flutter Future的正确用法
基于单片机测量空气粘滞系数方案
第五章 数据链路层与局域网
conda xgboost 安装 jupyter notebook
LeetCode 0179. 最大数
蓝桥杯历届试题-合根植物(并查集)
C-关键字之volatile
穿越派·派盘+KeePass = 最安全的私人密码管理方案
最新:新冠疫苗多久起效?能保护多久?
2017年8月历史文章汇总
多种决策树及应用 笔记
常用的正则表达式(不定期更新)