当前位置:网站首页>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
边栏推荐
- Gbase 8C database object size function (I)
- Domain Driven Design -- Terminology
- JS数字精度丢失的原因及解决方案
- 小散量化炒股记|量化系统中数据是源头,教你搭建一款普适的数据源框架
- Gbase 8C configuration setting function
- 自定义类型:结构体,枚举,联合
- 递归的使用:1.将平铺数组转为树 2.将树转化为平铺数组
- Gbase 8C transaction ID and snapshot (V)
- Enterprise operation and maintenance practice - using aliyun container image service to pull and build images of overseas GCR and quay warehouses
- Fluorite network, difficult to be a "lone brave"
猜你喜欢
![[interview: concurrent article 28:volatile] orderliness](/img/8d/c4c2ca08d8883e997709d208b7395b.png)
[interview: concurrent article 28:volatile] orderliness

Leetcode: 515. Find the maximum value in each tree row

Enterprise operation and maintenance practice - using aliyun container image service to pull and build images of overseas GCR and quay warehouses

Linux安装mysql8.0.29详细教程

hypermesh 圆周阵列-插件

Open source flight control (Px4, ardupilot)

54:第五章:开发admin管理服务:7:人脸入库流程;人脸登录流程;浏览器开启视频调试模式(以便能够在本机的不安全域名的情况下,也能去开启摄像头);

Blizzard Diablo 4 ps5 / PS4 beta added to Playstation database

LeetCode高频题128. 最长连续序列,经常被互联网大厂面试考到

Fluorite network, difficult to be a "lone brave"
随机推荐
数据输出-绘制动图
What is method and methodology: understand the underlying logic of self-improvement
Software testing interview question: why should we carry out testing in a team?
day7
Leveraging the blue ocean of household appliances consumption with "digital channels", the dealer online system enables enterprises to further their business
Completely delete MySQL in Linux system
N32l43x FLASH read \ write \ erase operation summary
Gbase 8C recovery control function
ros2 launch文件常用模块
Favorite songs
JS what situations can't use json Parse, json.stringify deep copy and a better deep copy method
简单为美-编程思路
FreeRTOS内核小结
萤石网络,难当「孤勇者」
GBase 8c 快照同步函数
Gbase 8C transaction ID and snapshot (VI)
实时数仓:美团的实时数仓建设实践
The storage cost is reduced by 80%. How does the cost management of youzan data middle office do?
Gbase 8C backup control function (II)
The petrochemical industry is facing the tide of rising prices, and the digital dealer distribution system platform enables dealers and stores