当前位置:网站首页>Static attribute, super()
Static attribute, super()
2022-07-24 12:43:00 【Liang wha duck,】
Static attribute : Properties that can only be called by classes
stay ES5 in Static attribute writing
function MyClass() {
}
// ES5 Static attribute writing
/*MyClass.say = function () {
console.log(' This is a static method ');
}
const obj = new MyClass();*/
stay ES6 in Static attribute writing
// ES6
/*class MyClass {
constructor() {
}
// Static methods Only static methods have no static properties
static say() {
}
}*/
super()
Case one : super When called as a function , Represents the constructor of the parent class .
ES6 In the request , The constructor for the subclass must be executed once super function
As a function ,super() Only in the constructor of subclasses , If it is used in other places, it will report an error
Class A {
constructor() {
}
}
Class B extends A {
constructor() {
super();
}
}
【 notes 】super Although it represents the parent class A Constructor for , But it returns a subclass B Example , namely super Inside this Pointing to B. therefore super() In this case A.prototype.constructor.call(this)
class FatherClass {
// constructor Don't write , stay new It will be added by default
// But when it comes to inheritance , A subclass constructor Is a must , Otherwise, the report will be wrong , because constructor Medium super It's a must , therefore constructor Must also write
}
class MyClass2 extends FatherClass{
constructor() {
super(); // This time super Equivalent to the parent class Class
// Why do I have to call this step ??
// stay newFatherClass When this example , An object will be generated by default ,this Will point to this object .
// But use inheritance , If you don't call super, Then this subclass will not generate objects , There won't be this, No, this The instance , It cannot be returned
// No, this, Then you can't give him attributes (this. attribute ), It can't be used as a class
// Therefore, this step must be written in addition to inheriting the private attributes of the parent class , Also let subclasses have this object
// super() This step is equivalent to ES5 Medium FatherClass.call(this);
}
// Static methods No static properties
static say() {
}
}
The second case : super As an object , In the ordinary way , Point to the prototype of the parent class ; In a static method , Point to the parent class
Class A {
P() {
return l2;
}
}
Class B extends A {
constructor() {
super();
console.log(super.p()); // 2
}
}
let b = new B();
In the above code , Subclass B Medium super.p(), Will be super Used as an object , At this time ,super In the ordinary way , Point to A.prototype, therefore super.p() amount to A.prototype.p()
because this Pointing subclass , So if it goes through super Assign a value to an attribute , At this time super Namely this, The assigned property becomes a property of the subclass instance
class FatherClass {
}
class MyClass2 extends FatherClass{
constructor() {
super(); // This time super Point to the parent class , amount to Parent class ()
super.say(); // amount to FatherClass.prototype.say()
}
// Static methods No static properties
static say() {
// super Can appear here , But it cannot be called as a function , When called as a function, it can only appear in constructor in
// but super This keyword can appear in prototype methods and static methods
// there super Point to the parent class
// This also points to the parent class ? Here is the call Parent class . Static methods , And above it is Parent class ()
}
// Prototype method
add() {
// there super Point to the prototype of the parent class .FatherClass.prototype
}
}
边栏推荐
- 【Rust】引用和借用,字符串切片 (slice) 类型 (&str)——Rust语言基础12
- ThinkPHP realizes database backup
- Buckle exercise - 32 divided into k equal subsets
- Wechat applet learning five page Jump methods
- 使用Jenkins搭建CI服务器
- 如何将Typora中图片上传到csdn
- What can breaking through the memory wall bring? See the actual battle of volcano engine intelligent recommendation service to save money and increase efficiency
- QT notes - sort a column specified by qtablewidget
- Learn the calculation method of quantile value in n minutes
- Summary of MySQL database combined with actual SQL optimization of the project
猜你喜欢

自己实现is_default_constructible

Implementing deep learning framework from zero -- further exploration of the implementation of multilayer bidirectional RNN

基于Kubernetes v1.24.0的集群搭建(三)

Everything about native crash

6-16 vulnerability exploitation -rlogin maximum permission login

English语法_不定代词 - 概述

Online XML to CSV tool

How QT creator changes the default build directory

Node takes effect after using NVM to install under Windows system, but NPM does not take effect
EfficientFormer:轻量化ViT Backbone
随机推荐
What can breaking through the memory wall bring? See the actual battle of volcano engine intelligent recommendation service to save money and increase efficiency
Conference publishing function of conference OA project
for mysql
国产旗舰手机定价近六千,却连iPhone12都打不过,用户选谁很明确
The sixth question of ape Anthropology
Ansible的安装及部署
QT notes - sort a column specified by qtablewidget
The seventh topic of ape Anthropology
How to mount NFS shares using autofs
The basis of point graph in the map of life information and knowledge
Equal principal increasing repayment / equal principal decreasing mortgage repayment calculator
我在一个模块工程中使用注解配置了redis的序列化, 然后在另外一个模块引入这个模块,为什么这个配置
1.9. 触摸按钮(touch pad)测试
Buckle practice - 31 effective tic tac toe games
权限系统就该这么设计,yyds
有没有2、3w前期适合一个人干的创业项目呢?做自媒体可以吗?
QWaitCondition 的正确使用方法
Intent jump pass list set
Use typeface to set the text font of textview
EfficientFormer:轻量化ViT Backbone