当前位置:网站首页>Key points of ES6 class inheritance
Key points of ES6 class inheritance
2022-07-27 10:19:00 【InfoQ】
How to write it
- Constructor method
- Example method
- Get function
- Set function
- Static class methods
constructor
class Person {
constructor(name) {
this.name = name
console.log('person ctor');
}
}
let p1 = new Person("p1")
function Person(name){
this.name = name
console.log('person ctor')
}
let p1 = new Person("p1")
class Animal {}
let a = Animal(); // TypeError: class constructor Animal cannot be invoked without 'new'
characteristic
class Person {}
console.log(typeof Person); // function
class Person {}
let p = new Person()
console.log(p instanceof Person); // true
class Person {
constructor() {
this.name = new String('Jack');
this.sayName = () => console.log(this.name);
}
}
let p1 = new Person();
let p2 = new Person();
console.log(p1.name === p2.name) // false
console.log(p1.sayName === p2.sayName) // false
class Person {
constructor() {
this.name = new String('Jack');
}
sayName(){
console.log(this.name);
}
}
let p1 = new Person();
let p2 = new Person();
console.log(p1.sayName === p2.sayName) // true
Inherit
function SuperType(){}
SuperType.prototype.sayName = ()=>{console.log("bob")}
function SubType(){
SuperType.call(this) // Constructor inheritance
}
let p1 = new SubType()
console.log(p1.sayName()) // Uncaught TypeError: p1.sayName is not a function
function SuperType(){}
SuperType.prototype.sayName = ()=>{console.log("bob")}
function SubType(){}
SubType.prototype = new SuperType() // Prototype chain inheritance
let p1 = new SubType()
console.log(p1.sayName()) // bob
function SuperType(){
this.name = ["bob","tom"];
}
function SubType(){}
SubType.prototype = new SuperType() // Prototype chain inheritance
let p1 = new SubType()
p1.name.push("jerry")
let p2 = new SubType()
console.log(p2.name) // ['bob', 'tom', 'jerry']
function SuperType(){
this.name = ["bob","tom"];
}
function SubType(){
SuperType.call(this) // Constructor inheritance
}
let p1 = new SubType()
p1.name.push("jerry")
let p2 = new SubType()
console.log(p2.name) // ['bob', 'tom']
class SuperType{}
SuperType.prototype.sayName = ()=>{console.log("bob")}
class SubType extends SuperType{
}
let p1 = new SubType()
p1.sayName() // bob
class SuperType{
constructor(){
this.name=["bob","tom"]
}
}
class SubType extends SuperType{
}
let p1 = new SubType()
let p2 = new SubType()
p1.name.push("Jerry")
console.log(p2.name) // ['bob', 'tom']
Digression
边栏推荐
- Fsm onehot 答题记录
- 活体检测综述
- Matlab-基于短时神经网络的声音分类
- 数学推理题:张王李赵陈五对夫妇聚会,见面握手
- Decision tree principle and case application - Titanic survival prediction
- mount.nfs: access denied by server while mounting解决
- pytorch中对BatchNorm2d()函数的理解
- 达梦 PARTGROUPDEF是自定义的对象吗?
- hugo学习笔记
- Practice and exploration of overseas site Seata of ant group
猜你喜欢

DCGAN论文改进之处+简化代码

Oracle查看硬解析
![Shell函数、系统函数、basename [string / pathname] [suffix] 可以理解为取路径里的文件名称 、dirname 文件绝对路径、自定义函数](/img/3d/d7276d2010f1d77a3bd572cc66eced.png)
Shell函数、系统函数、basename [string / pathname] [suffix] 可以理解为取路径里的文件名称 、dirname 文件绝对路径、自定义函数

Vs2019 Community Edition Download tutorial (detailed)

Food safety | is sugar free really sugar free? These truths need to be known

VS2019+CUDA11.1新建项目里没有CUDA选项

数据库性能系列之子查询

Switch port mirroring Configuration Guide

3D人脸重建:Joint 3D Face Reconstruction and Dense Alignment with position Map Regression Network

Open3d library installation, CONDA common instructions, importing open3d times this error solving environment: failed with initial frozen solve Retrying w
随机推荐
[scm] source code management - lock of perforce branch
Shell的read 读取控制台输入、read的使用
About new_ Online_ Judge_ 1081_ Thoughts on Goldbach's conjecture
How does data analysis solve business problems? Here is a super detailed introduction
vs2019社区版下载教程(详细)
After one year, the paper was finally accepted by the international summit
使用 LSM-Tree 思想基于.NET 6.0 C# 写个 KV 数据库(案例版)
Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0
Fsm onehot 答题记录
3D人脸重建:Joint 3D Face Reconstruction and Dense Alignment with position Map Regression Network
Color segmentation using kmeans clustering
线代003
Shell变量、系统预定义变量$HOME、$PWD、$SHELL、$USER、自定义变量、特殊变量$n、$#、$*、[email protected]、$?、env看所有的全局变量值、set看所有变量
Acl2021 best paper released, from ByteDance
Mysql database experiment training 5, data query YGGL database query (detailed)
数据库性能系列之子查询
samba服务器
Provincial Emergency Management Department: Guangzhou can strive to promote the experience of emergency safety education for children
卸载CUDA11.1
Anaconda installation (very detailed)