当前位置:网站首页>原型链继承和构造函数继承的 “毛病”
原型链继承和构造函数继承的 “毛病”
2022-07-29 09:07:00 【InfoQ】
原型链继承
function SuperType() {
this.property = true;
}
function SubType() {
this.subproperty = false;
}
SuperType.prototype.getSuperValue = function() {
return this.property;
};
SubType.prototype.getSubValue = function () {
return this.subproperty;
};
SubType.prototype = new SuperType(); // 对 SubType 得原型链重新指定,是原型链继承
let instance = new SubType();
console.log(instance.getSuperValue()); // true
// 继承自祖先(遗产)
instance.__proto__ === SubType.prototype // true
SubType.prototype.__proto__ === SuperType.prototype // true
// 继承自上帝(天赋)
SuperType.__proto__ === Function.prototype // true
SubType.__proto__ === Function.prototype // true
SuperType.prototype.__proto__ === Object.prototype // true
Object.prototype.__proto__ === null // true
function SuperType() {
this.colors = ["red", "blue", "green"];
}
function SubType() {}
SubType.prototype = new SuperType() // 原型链继承
let s1 = new SubType()
let s2 = new SubType()
s1.colors.push("yellow")
console.log(s1.colors) // ['red', 'blue', 'green', 'yellow']
console.log(s2.colors) // ['red', 'blue', 'green', 'yellow']
SubType.prototype = new SuperType()
构造函数继承
function SuperType() {
this.colors = ["red", "blue", "green"];
}
function SubType() {
SuperType.call(this) // 构造函数继承
}
let s1 = new SubType()
let s2 = new SubType()
s1.colors.push("yellow")
console.log(s1.colors) // ['red', 'blue', 'green', 'yellow']
console.log(s2.colors) // ['red', 'blue', 'green']
function SuperType() {
}
function SubType() {
SuperType.call(this) // 构造函数继承
}
SuperType.prototype.fn = ()=>{}
let s1 = new SubType()
console.log(s1.fn) // undefined
function SuperType() {
this.fn=()=>{}
}
function SubType() {
SuperType.call(this) // 构造函数继承
}
let s1 = new SubType()
let s2 = new SubType()
console.log(s1.fn === s2.fn) // false
function SuperType() {}
function SubType() {}
SuperType.prototype.fn = ()=>{}
SubType.prototype = new SuperType() // 原型链继承
let s1 = new SubType()
console.log(s1.fn) // ()=>{}
function SuperType() {
this.fn=()=>{}
}
function SubType() {}
SubType.prototype = new SuperType() // 原型链继承
let s1 = new SubType()
let s2 = new SubType()
console.log(s1.fn === s2.fn) // true
- 原型链继承:所有继承的属性和方法都会在对象实例间共享,无法做到实例私有。
- 构造函数继承:子类不能访问父类原型上的方法。
组合继承
function SuperType(name){
this.name = name;
this.colors = ["red", "blue", "green"];
}
function SubType(name, age){
SuperType.call(this, name) // 构造函数继承
this.age = age;
}
SuperType.prototype.sayName = function() {
console.log(this.name);
}
SubType.prototype = new SuperType() // 原型链继承
SubType.prototype.sayAge = function() {
console.log(this.age);
}
let s1 = new SubType("Nicholas", 29)
let s2= new SubType("Greg", 27)
s1.colors.push("yellow")
console.log(s1.colors) // ['red', 'blue', 'green', 'yellow']
console.log(s2.colors) // ['red', 'blue', 'green']
s1.sayName() // Nicholas
s2.sayName() // Greg
s1.sayAge() // 29
s2.sayAge() // 27
边栏推荐
- [C language] DataGridView binding data
- How to quickly experience oneos
- Basic part 2 of flowable
- LeetCode力扣题目总结(题目编号:53、3、141、面试题022、剑指offer链表中环的入口节点、20、19、牛客NC1、103、1143、牛客127)
- Reptile practice (10): send daily news
- BI data analysis practitioners learn financial knowledge from scratch? What introductory books are recommended
- Intel will gradually end the optane storage business and will not develop new products in the future
- Restful style details
- Squareline partners with visual GUI development of oneos graphical components
- LeetCode刷题(6)
猜你喜欢
(视频+图文)机器学习入门系列-第3章 逻辑回归
Sword finger offer 26. substructure of tree
MySQL 错误总结
Quick sorting (quick sorting) (implemented in C language)
(视频+图文)机器学习入门系列-第1章 引言
Database system design: partition
The gold content of PMP certificate has been increased again and included in the scope of Beijing work residence permit
Emmet syntax
Common query optimization technology of data Lake - "deepnova developer community"
Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in
随机推荐
SAP sm30 brings out description or custom logical relationship
Could not receive a message from the daemon
Sword finger offer 50. the first character that appears only once
Mathematical modeling - Differential Equations
四元数与其在Unity中的简单应用
Demonstration and solution of dirty reading, unrepeatable reading and unreal reading
Simple unit testing idea
【Unity入门计划】C#与Unity-了解类和对象
2022年P气瓶充装考试模拟100题模拟考试平台操作
Opencv cvcircle function
Excellent package volume optimization tutorial
Retinal Vessel Segmentation via a Semantics and Multi-Scale Aggregation Network
Flowable 高级篇
GBase 8s数据库有哪些备份恢复方式
English high frequency suffix
The gold content of PMP certificate has been increased again and included in the scope of Beijing work residence permit
C language -- 22 one dimensional array
Several ways of debugging support under oneos
Emmet syntax
Redis series 3: highly available master-slave architecture