当前位置:网站首页>"Defects" of prototype chain inheritance and constructor inheritance
"Defects" of prototype chain inheritance and constructor inheritance
2022-07-29 09:17:00 【InfoQ】
Prototype chain inheritance
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(); // Yes SubType The prototype chain is reassigned , Is the prototype chain inheritance
let instance = new SubType();
console.log(instance.getSuperValue()); // true
// Inherited from ancestors ( heritage )
instance.__proto__ === SubType.prototype // true
SubType.prototype.__proto__ === SuperType.prototype // true
// Inherited from God ( talent )
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() // Prototype chain inheritance
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()Constructor inheritance
function SuperType() {
this.colors = ["red", "blue", "green"];
}
function SubType() {
SuperType.call(this) // Constructor inheritance
}
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) // Constructor inheritance
}
SuperType.prototype.fn = ()=>{}
let s1 = new SubType()
console.log(s1.fn) // undefined
function SuperType() {
this.fn=()=>{}
}
function SubType() {
SuperType.call(this) // Constructor inheritance
}
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() // Prototype chain inheritance
let s1 = new SubType()
console.log(s1.fn) // ()=>{}
function SuperType() {
this.fn=()=>{}
}
function SubType() {}
SubType.prototype = new SuperType() // Prototype chain inheritance
let s1 = new SubType()
let s2 = new SubType()
console.log(s1.fn === s2.fn) // true
- Prototype chain inheritance : All inherited properties and methods are shared between object instances , Can't do instance private .
- Constructor inheritance : Subclasses cannot access methods on parent prototypes .
Combination inheritance
function SuperType(name){
this.name = name;
this.colors = ["red", "blue", "green"];
}
function SubType(name, age){
SuperType.call(this, name) // Constructor inheritance
this.age = age;
}
SuperType.prototype.sayName = function() {
console.log(this.name);
}
SubType.prototype = new SuperType() // Prototype chain inheritance
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
边栏推荐
- English high frequency suffix
- Jetpack Glance? The spring of widgets is coming
- 浅谈契约测试
- Opencv cvcircle function
- 怎样查询快递物流筛选出无信息单号删除或者复制
- ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
- Complete knapsack problem from simplicity to ultimate
- How does xjson implement four operations?
- State compression DP
- SAP sm30 brings out description or custom logical relationship
猜你喜欢

Error reporting when adding fields to sap se11 transparent table: structural changes at the field level (conversion table xxxxx)

Discussion on the integration of storage and calculation and the calculation in storage

Leetcode question brushing (6)

Flowable 基础篇2

Retinal Vessel Segmentation via a Semantics and Multi-Scale Aggregation Network

C# 使用数据库对ListView控件数据绑定

Mathematical modeling clustering

MySQL的数据类型
Data representation and calculation (base)

2022 P cylinder filling test simulation 100 questions simulation test platform operation
随机推荐
1.2.24 fastjson deserialization templatesimpl uses chain analysis (very detailed)
AxureRP原型设计 快速开始
Excellent Allegro skill recommendation
[LOJ 6485] LJJ binomial theorem (unit root inversion) (template)
STM32 application development practice tutorial: design and implementation of controllable LED water lamp
[C language] DataGridView binding data
Could not receive a message from the daemon
What is the difference between the pre training model and the traditional method in sorting?
Floweable foundation Chapter 1
不用Swagger,那我用啥?
(视频+图文)机器学习入门系列-第2章 线性回归
Travel notes in 2022 (ongoing)
英语高频后缀
Parameter initialization
How to change MySQL into Chinese
Quick sorting (quick sorting) (implemented in C language)
Asp graduation project - based on C # +asp Net+sqlserver laboratory reservation system design and Implementation (graduation thesis + program source code) - Laboratory Reservation System
Notes on network principles (five layer network)
Network knowledge summary
2022危险化学品经营单位主要负责人操作证考试题库及答案