当前位置:网站首页>"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
边栏推荐
- [performance optimization methodology series] III. core idea of performance optimization (2)
- mysql怎么换成中文
- AI is at the forefront | focusing on natural language processing, machine learning and other fields; From Fudan University, Institute of automation, Chinese Academy of Sciences and other teams
- Opencv cvcircle function
- Sword finger offer 26. substructure of tree
- Data is the main body of future world development, and data security should be raised to the national strategic level
- How to change MySQL into Chinese
- Summary of research on endogenous information security technology of industrial measurement and control equipment
- Solve the problem of false Base64 character in Base64
- Simple unit testing idea
猜你喜欢

mysql怎么换成中文

C language -- 22 one dimensional array

On the charm of code language

浅谈契约测试

Outlook tutorial, how to create an electronic signature in outlook?

2022电工(初级)考题模拟考试平台操作

Excellent package volume optimization tutorial

(Video + graphic) introduction to machine learning series - Chapter 3 logical regression

Restful style details

存算一体与存内计算计算杂谈
随机推荐
Memory leaks and common solutions
基于C语言实现的NFA确定化和DFA最小化
What are the backup and recovery methods of gbase 8s database
完全背包问题 从朴素到终极
优秀的Allegro Skill推荐
Design of distributed (cluster) file system
浅谈契约测试
(视频+图文)机器学习入门系列-第1章 引言
Opencv cvcircle function
AI is at the forefront | focusing on natural language processing, machine learning and other fields; From Fudan University, Institute of automation, Chinese Academy of Sciences and other teams
SAP sm30 brings out description or custom logical relationship
Introduction to translation professional qualification (level) examination
Amazfit dial toolbox Online
ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
Flowable 基础篇2
What is the difference between the pre training model and the traditional method in sorting?
Amazfit dial Kit
Quaternion and its simple application in unity
2022 Shandong Province safety officer C certificate work certificate question bank and answers
Simple unit testing idea