当前位置:网站首页>Typescript中类的使用
Typescript中类的使用
2022-07-28 00:14:00 【RemoteDev】
/*
* 基类
* */
class Base{
constructor(obj=null) {
if (obj){
console.log('子类调用,基类构造函数',obj.a,obj.b);
}else{
console.log('基类构造函数');
}
}
toString(obj){
let [a,b]=[obj.a,obj.b];
console.log('子类调用:toString()===>',a,b);
}
}
/*
* 派生类,继承于Base
* */
class Framework extends Base{
constructor({a=0,b=0}={}) {
super({a,b});//向基类传入参数
super.toString({a,b});//调用基类函数
}
//公有方法实现,默认下类的所有方法都是公有的
getParentName(){
}
//类的私有方法实现
_getParentName(){
}
}
let base = new Base();//实例化基类
let framework = new Framework({a:8,b:9});//实例化派生类
//类实例是对象,类是函数
console.log('类对象的数据类型:',typeof base,typeof framework,'类的数据类型',typeof Base,typeof Framework);
console.log('类原型对象构造:',Framework.prototype.constructor,'类构造:',Framework.constructor);
console.log('类的原型对象prototype的constructor指向类名:',Framework.prototype.constructor===Framework);//类的原型对象prototype的constructor指向类名
console.log('类名:',Framework.name);
console.log(framework.constructor===Framework.prototype.constructor);
//通过类原型对象向类添加方法
Base.prototype.Add = function (x,y) {
return x+y;
}
console.log('调用添加到基类原型对象上的方法:',base.Add(1,2));//调用添加到基类原型对象上的方法
console.log('派生类调用基类原型对象的方法:',framework.Add(5,6));//派生类调用基类原型对象的方法
// @ts-ignore
Base.prototype.test =()=> {
console.log('test func');
};
console.log(Object.getOwnPropertyNames(Base.prototype));//取类所有方法名
//使用Object.assign为类添加方法
Object.assign(Base.prototype, {
M1() {
console.log('M1');
},
M2() {
console.log('M2');
},
M3() {
console.log('M3');
}
});
//调用添加的方法
base.M1();
framework.M2();
framework.M3();
//注: 类内部定义的所有方法都是不可枚举的
console.log(Object.keys(Base.prototype));//constructor,toString这两个方法都在类内部定义,所以Object.keys无法枚举
//使用变量名定义类方法
let method = 'Done';
Object.assign( Base.prototype,{[method](){
console.log('通过变量名定义的函数');
}});
console.log(Object.keys(Base.prototype));
base.Done();//通过方法名调用
Reflect.get(base,method)();//使用反射通过变量名调用
framework.Done();
console.log(Object.getOwnPropertyNames(Framework.prototype));
console.log('取当前类的继承对象',Object.getPrototypeOf(framework));
console.log('取当前类的继承对象的所有方法',Object.getPrototypeOf( Object.getPrototypeOf(framework)));
//根据子类取得基类,并调用基类方法
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对象原型',base.__proto__,'framework对象原型:',framework.__proto__);
//通过表达式形式声明类
const ExpClass = class Exp {//此处Exp为类的别名,作用域只限于类内部定义
constructor() {
console.log('构造时打印类名:',Exp.name);
}
getClassName(){
console.log('调用getClassName打印类名:');
return Exp.name;
}
}
let exp = new ExpClass();
console.log(exp.getClassName());
//立即执行类
let execClass = new class{
constructor() {
console.log('立即执行类');
}
}();
//带参数的立即执行类
let execClassWithParam = new class{
constructor(param) {
console.log('立即执行类,参数:',param);
}
}('RemoteDev');
//注: 类不存在变量提升,必须先声明后再实例化
边栏推荐
- Leveraging the blue ocean of household appliances consumption with "digital channels", the dealer online system enables enterprises to further their business
- GBase 8c 配置设置函数
- Gbase 8C backup control function (IV)
- 企业运维实践-使用Aliyun容器镜像服务对海外gcr、quay仓库镜像进行镜像拉取构建
- HCIP第十三天笔记
- In the era of great changes in material enterprises, SRM supplier procurement system helps enterprises build a digital benchmark for property procurement
- Linux安装mysql8.0.29详细教程
- GBase 8c 恢复控制函数
- Fiddler 手机抓包代理设置(针对华为荣耀60S)
- GBase 8c 备份控制函数(三)
猜你喜欢

Lambda expressions and stream streams

After learning the loop, I came across the problem of writing factorial of N, which caused a series of problems, including some common pitfalls for beginners, and how to simplify the code

The storage cost is reduced by 80%. How does the cost management of youzan data middle office do?

Blizzard Diablo 4 ps5 / PS4 beta added to Playstation database

Linux系统彻底删除Mysql

HyperMesh circular array - plug in

Machine learning how to achieve epidemic visualization -- epidemic data analysis and prediction practice

leetcode: 515. 在每个树行中找最大值

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

基于 Flink CDC 实现海量数据的实时同步和转换
随机推荐
##ELK日志分析系统搭建##
Flink 在 讯飞 AI 营销业务的实时数据分析实践
什么是方法,什么是方法论:了解自我精进提升的底层逻辑
Packet capturing wizard netcapture app packet capturing tutorial "complete"
Gbase 8C snapshot synchronization function
GBase 8c 事务ID和快照(三)
Recursion related exercises
实时数仓:美团的实时数仓建设实践
以“数字化渠道”撬动家用电器消费蓝海,经销商在线系统让企业生意更进一步
【taichi】在太极中画出规整的网格
JS what situations can't use json Parse, json.stringify deep copy and a better deep copy method
Enterprise operation and maintenance practice - using aliyun container image service to pull and build images of overseas GCR and quay warehouses
The story of the third uncle
Gbase 8C backup control function (II)
2.2 comprehensive application questions - sequence table
Comparison between hardware SPI and software analog SPI rate
ros2 launch文件常用模块
The storage cost is reduced by 80%. How does the cost management of youzan data middle office do?
"Do you" want to be a test / development programmer? We strive to sprout
石油化工行业迎战涨价大潮,经销商分销系统平台数字化赋能经销商与门店