当前位置:网站首页>es6 class 继承的重点
es6 class 继承的重点
2022-07-27 09:58:00 【InfoQ】
写法
- 构造函数方法
- 实例方法
- 获取函数
- 设置函数
- 静态类方法
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'
特性
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
继承
function SuperType(){}
SuperType.prototype.sayName = ()=>{console.log("bob")}
function SubType(){
SuperType.call(this) // 构造函数继承
}
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() // 原型链继承
let p1 = new SubType()
console.log(p1.sayName()) // bob
function SuperType(){
this.name = ["bob","tom"];
}
function SubType(){}
SubType.prototype = new SuperType() // 原型链继承
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) // 构造函数继承
}
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']
题外话
边栏推荐
- 语音识别的一些开源项目整理
- StyleGAN论文笔记+修改代码尝试3D点云生成
- Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号
- Discussion on a problem
- Food safety | the kitchen board environment is very important. Do you know these use details?
- 超赞的卡尔曼滤波详解文章
- LeetCode.1260. 二维网格迁移____原地暴力 / 降维+循环数组直接定位
- DCGAN论文改进之处+简化代码
- Introduction to regular expressions of shell, general matching, special characters: ^, $,., * Character range (brackets): [], special characters: \, matching mobile phone number
- Metaspolit
猜你喜欢

Xiandai 003

How does data analysis solve business problems? Here is a super detailed introduction

Snowflake vs. databricks who is better? The latest war report in 2022

直播倒计时 3 天|SOFAChannel#29 基于 P2P 的文件和镜像加速系统 Dragonfly

Metaaploit-后渗透技知识

pillow的原因ImportError: cannot import name ‘PILLOW_VERSION‘ from ‘PIL‘,如何安装pillow<7.0.0

Shell的read 读取控制台输入、read的使用

备战金九银十Android面试准备(含面试全流程,面试准备工作面试题和资料等)

Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0

怎样关闭电脑开机自启动的应用
随机推荐
数学推理题:张王李赵陈五对夫妇聚会,见面握手
Oracle RAC 19C PDB instance is down
Example of ICP registration for PCL
Girl fan wants to find a boyfriend, but it's for
sql注入
Snowflake vs. databricks who is better? The latest war report in 2022
Food safety | the kitchen board environment is very important. Do you know these use details?
QT learning (II) -- a brief introduction to QT Creator
Matlab- draw superimposed ladder diagram and line diagram
There is no CUDA option in vs2019+cuda11.1 new project
Pytorch installation (very detailed)
Matlab-基于短时神经网络的声音分类
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量
Shell integrated application cases, archiving files, sending messages
活体检测综述
并发之park与unpark说明
Engineering survey simulation volume a
Leetcode.814. binary tree pruning____ DFS
Leetcode.1260. 2D grid migration____ In situ violence / dimensionality reduction + direct positioning of circular array
Ant advanced task