当前位置:网站首页>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']
题外话
边栏推荐
- DCGAN论文改进之处+简化代码
- 安装CUDA失败的情况nsight visual studio edition失败
- 学习Typescript(一)
- [scm] source code management - lock of perforce branch
- Interview Essentials: shrimp skin server 15 consecutive questions
- samba服务器
- Case of burr (bulge) notch (depression) detection of circular workpiece
- Concurrent thread state transition
- ACL2021最佳论文出炉,来自字节跳动
- 3D face reconstruction and dense alignment with position map progression network
猜你喜欢

怎样关闭电脑开机自启动的应用

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

open3d库的安装,conda常用指令,导入open3d时报这个错误Solving environment: failed with initial frozen solve. Retrying w

线代004

Explain knative cloud function framework in simple terms!

卸载CUDA11.1

hdu5288(OO’s Sequence)

Discussion on a problem

oracle rac 19c pdb实例当掉

并发之park与unpark说明
随机推荐
Anchor free detector: centernet
Ant高级-path和fileset
hdu5288(OO’s Sequence)
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量
Pyautogui实现自动化办公-RPA小case
Shell运算符、$((运算式))” 或 “$[运算式]、expr方法、条件判断、test condition、[ condition ]、两个整数之间比较、按照文件权限进行判断、按照文件类型进行判断
文件上传漏洞绕过方法
After one year, the paper was finally accepted by the international summit
There is no CUDA option in vs2019+cuda11.1 new project
Fsm onehot 答题记录
Mysql database experiment training 5, data query YGGL database query (detailed)
Mathematical reasoning: five couples get together and shake hands when they meet
Summary of binary tree exercises
Cannot start after installing MySQL 5.7.27 in CentOS 7? (Language bash)
Qt 学习(二) —— Qt Creator简单介绍
安装CUDA失败的情况nsight visual studio edition失败
Shell variables, system predefined variables $home, $pwd, $shell, $user, custom variables, special variables $n, $, $*, [email protected],
Qt 学习(二) —— .pro文件详解
Simple use of tflite
备战金九银十Android面试准备(含面试全流程,面试准备工作面试题和资料等)