当前位置:网站首页>"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
边栏推荐
- STM32 application development practice tutorial: design and implementation of controllable LED water lamp
- The biggest upgrade of Bluetooth over the years: Bluetooth Le audio is about to appear in all kinds of digital products
- Excellent Allegro skill recommendation
- C # use restsharp library to realize post request
- 乱打日志的男孩运气怎么样我不知道,加班肯定很多
- Rocky基础之编译安装apache
- Flowable UI制作流程图
- No duplicate data in the same field of different databases
- Leetcode question brushing (6)
- Want to know how to open an account through online stock? Excuse me, is it safe to open a stock account by mobile phone?
猜你喜欢

I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime

Quick sorting (quick sorting) (implemented in C language)

C language -- 22 one dimensional array

The use and Simulation of string function, character function and memory function

【Unity入门计划】C#与Unity-了解类和对象

Emmet syntax

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

What is the difference between the pre training model and the traditional method in sorting?

(视频+图文)机器学习入门系列-第1章 引言

Flowable 高级篇
随机推荐
2022危险化学品经营单位主要负责人操作证考试题库及答案
C # use restsharp library to realize post request
四元数与其在Unity中的简单应用
2022 R2 mobile pressure vessel filling test question simulation test platform operation
网络原理笔记(五层网络)
How does alternates achieve high-performance publish and subscribe?
不用Swagger,那我用啥?
Flowable 高级篇
(视频+图文)机器学习入门系列-第1章 引言
What are the backup and recovery methods of gbase 8s database
怎样查询快递物流筛选出无信息单号删除或者复制
Introduction to translation professional qualification (level) examination
状态压缩dp
Leetcode:132. split palindrome string II
云原生管理实践:业务引领的DevOps持续交付体系
AxureRP原型设计 快速开始
Sublime text create page
[C language] DataGridView binding data
Design of distributed (cluster) file system
Simple unit testing idea