当前位置:网站首页>Use of classes in typescript
Use of classes in typescript
2022-07-28 02:01:00 【RemoteDev】
/*
* Base class
* */
class Base{
constructor(obj=null) {
if (obj){
console.log(' Subclass call , Base class constructor ',obj.a,obj.b);
}else{
console.log(' Base class constructor ');
}
}
toString(obj){
let [a,b]=[obj.a,obj.b];
console.log(' Subclass call :toString()===>',a,b);
}
}
/*
* Derived class , Inherited from Base
* */
class Framework extends Base{
constructor({a=0,b=0}={}) {
super({a,b});// Pass parameters to the base class
super.toString({a,b});// Call the base class function
}
// Public method implementation , By default, all methods of the class are public
getParentName(){
}
// Private method implementation of class
_getParentName(){
}
}
let base = new Base();// Instantiate base class
let framework = new Framework({a:8,b:9});// Instantiate derived classes
// Class instances are objects , Classes are functions
console.log(' Data type of class object :',typeof base,typeof framework,' The data type of the class ',typeof Base,typeof Framework);
console.log(' Class prototype object construction :',Framework.prototype.constructor,' Class construction :',Framework.constructor);
console.log(' Class prototype Of constructor Point to the class name :',Framework.prototype.constructor===Framework);// Class prototype Of constructor Point to the class name
console.log(' Class name :',Framework.name);
console.log(framework.constructor===Framework.prototype.constructor);
// Add methods to the class through the class prototype object
Base.prototype.Add = function (x,y) {
return x+y;
}
console.log(' Call the method added to the base class prototype object :',base.Add(1,2));// Call the method added to the base class prototype object
console.log(' The derived class calls the method of the base class prototype object :',framework.Add(5,6));// The derived class calls the method of the base class prototype object
// @ts-ignore
Base.prototype.test =()=> {
console.log('test func');
};
console.log(Object.getOwnPropertyNames(Base.prototype));// Take all method names of the class
// Use Object.assign Add methods to the class
Object.assign(Base.prototype, {
M1() {
console.log('M1');
},
M2() {
console.log('M2');
},
M3() {
console.log('M3');
}
});
// Call the added method
base.M1();
framework.M2();
framework.M3();
// notes : All methods defined inside the class are non enumerable
console.log(Object.keys(Base.prototype));//constructor,toString Both methods are defined inside the class , therefore Object.keys Can't enumerate
// Use variable names to define class methods
let method = 'Done';
Object.assign( Base.prototype,{[method](){
console.log(' Functions defined by variable names ');
}});
console.log(Object.keys(Base.prototype));
base.Done();// Call by method name
Reflect.get(base,method)();// Use reflection to call through variable names
framework.Done();
console.log(Object.getOwnPropertyNames(Framework.prototype));
console.log(' Take the inheritance object of the current class ',Object.getPrototypeOf(framework));
console.log(' Take all methods of the inherited object of the current class ',Object.getPrototypeOf( Object.getPrototypeOf(framework)));
// Get the base class according to the subclass , And call the base class method
let p = Object.getPrototypeOf( Object.getPrototypeOf(framework))
p.Done();
console.log(Object.getOwnPropertyNames(p));
console.log(Reflect.ownKeys(p));
Reflect.get(p,method)();
console.log('base Object prototype ',base.__proto__,'framework Object prototype :',framework.__proto__);
// Declare classes in the form of expressions
const ExpClass = class Exp {// here Exp Is the alias of the class , Scope is limited to the internal definition of the class
constructor() {
console.log(' Print the class name when constructing :',Exp.name);
}
getClassName(){
console.log(' call getClassName Print the name of the class :');
return Exp.name;
}
}
let exp = new ExpClass();
console.log(exp.getClassName());
// Execute class immediately
let execClass = new class{
constructor() {
console.log(' Execute class immediately ');
}
}();
// Immediate execution class with parameters
let execClassWithParam = new class{
constructor(param) {
console.log(' Execute class immediately , Parameters :',param);
}
}('RemoteDev');
// notes : Class does not have variable Promotion , You must declare before instantiating
边栏推荐
- How tormenting are weekly and monthly reports? Universal report template recommended collection! (template attached)
- 【taichi】在太极中画出规整的网格
- The petrochemical industry is facing the tide of rising prices, and the digital dealer distribution system platform enables dealers and stores
- GBase 8c 事务ID和快照
- Gbase 8C transaction ID and snapshot (V)
- Leveraging the blue ocean of household appliances consumption with "digital channels", the dealer online system enables enterprises to further their business
- Causes and solutions of JS digital accuracy loss
- 物企大变局时代,SRM供应商采购系统助力企业打造物业采购数字化标杆
- UE4 unreal ndisplay plug-in easy to use three fold screen details
- 抓包精灵NetCapture APP抓包教程《齐全》
猜你喜欢

EEG多元模式分析预测慈善捐赠行为

HCIP第十二天笔记

ue4 unreal NDisplay插件 简易使用 三折幕 详细...

Establishment of elk log analysis system
![[Taichi] draw a regular grid in Tai Chi](/img/48/14e825562afa3ffba96296799617f7.png)
[Taichi] draw a regular grid in Tai Chi
![[interview: concurrent article 28:volatile] orderliness](/img/8d/c4c2ca08d8883e997709d208b7395b.png)
[interview: concurrent article 28:volatile] orderliness

Comparison between hardware SPI and software analog SPI rate

53:第五章:开发admin管理服务:6:开发【admin管理员退出登录,接口】;(一个点:我们想要修改一个采用了某种编码方式的值时,新的值最好也按照这种编码方式编码后,再去修改;)

Forget the root password

UE4 unreal ndisplay plug-in easy to use three fold screen details
随机推荐
Fluorite network, difficult to be a "lone brave"
In the era of great changes in material enterprises, SRM supplier procurement system helps enterprises build a digital benchmark for property procurement
Cloud native enthusiast weekly: the evolution of Prometheus architecture
54:第五章:开发admin管理服务:7:人脸入库流程;人脸登录流程;浏览器开启视频调试模式(以便能够在本机的不安全域名的情况下,也能去开启摄像头);
[interview: concurrent article 28:volatile] orderliness
Software testing interview question: what do you think is the key to good test case design?
GBase 8c 事务ID和快照(六)
Typescript中类的使用
企业运维实践-使用Aliyun容器镜像服务对海外gcr、quay仓库镜像进行镜像拉取构建
##ELK日志分析系统搭建##
[Taichi] draw a regular grid in Tai Chi
JS what situations can't use json Parse, json.stringify deep copy and a better deep copy method
53:第五章:开发admin管理服务:6:开发【admin管理员退出登录,接口】;(一个点:我们想要修改一个采用了某种编码方式的值时,新的值最好也按照这种编码方式编码后,再去修改;)
嵌入式经典通信协议
网易云仿写
UE4 unreal ndisplay plug-in easy to use three fold screen details
数据输出-图片注释、标注
GBase 8c 备份控制函数(二)
Real time synchronization and conversion of massive data based on Flink CDC
Enterprise operation and maintenance practice - using aliyun container image service to pull and build images of overseas GCR and quay warehouses