当前位置:网站首页>Use of typescript class
Use of typescript class
2022-07-28 23:28: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);
}
}
// Private method name symbol definition
let s1 = Symbol('privateM1');
let s2 = Symbol('privateM2');
// Private attribute name symbol definition
let p1 = Symbol('privateP1');
/*
* 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
this[p1]='RemoteDev';
console.log(' Private attribute output :',this[p1]);
}
getPrivateProto(){
return this[p1];
}
setPrivateProto(v){
this[p1] = v;
}
// Public method implementation , By default, all methods of the class are public
getParentName(){
console.log('Public Method Call ...');
outOfClassMethod();// Call out of class methods , In this way, class objects cannot be accessed outOfClassMethod
}
// Private method implementation of class
_getParentName(){
console.log('Private Method Call ...');
}
call_out_method(obj){
outOfClassMethod.call(this,obj)// The method binding form outside the class implements the private method
}
// Call through Symbol Defined private methods
call_private(){
this[s1]('hello');
this[s2]('world');
}
// adopt Symbol Formal implementation class private methods , Class object cannot be accessed through Symbol The way of naming
[s1](obj){
console.log(' adopt Symbol Named private method :',s1,obj)
}
[s2](obj){
console.log(' adopt Symbol Named private method :',s2,obj)
}
// Another way to write private properties and private methods of classes ,# For private , At present, it is in the proposal stage
// #x=0;
// #getx(){return #x;}
}
function outOfClassMethod(obj=null){
console.log(" Realize the private method of the class by calling the method outside the class ",obj);
}
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
framework.getParentName();
framework._getParentName();//unsafe It can still be called outside the class
framework.call_out_method({x:'hello'});
console.log(Object.getOwnPropertyNames(Framework.prototype));
console.log(Object.getPrototypeOf(framework));// Base class
console.log(Object.keys(framework));// Class internal definition methods cannot be enumerated , So for []
framework.call_private();// Access private methods through public methods
console.log( ' Read private attribute values :',framework.getPrivateProto());
framework.setPrivateProto('NICK');// Write private attribute values
console.log( ' Read private attribute values :',framework.getPrivateProto());边栏推荐
- Elements in the middle (one article is enough)
- All aspect visual monitoring of istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
- Typescript防止基类被实例化
- RouYi-Cloud平台 ---项目的启动、登录功能是怎么实现的、怎么样创建新模块
- (important) first knowledge of C language -- function
- After reading MySQL database advanced practice (SQL xiaoxuzhu)
- Assembly analysis swift polymorphism principle
- [mongodb] basic use of mongodb database, special cases, and the installation and creation process of mongoose (including the installation of mongoose fixed version)
- Research on cookies in WebView
- [image segmentation] vein segmentation based on directional valley detection with matlab code
猜你喜欢

WebView optimization

安全狗入选《云安全全景图2.0》多个细项

Applet, JS, transfer object jump transfer parameter problem

以价换量却遭遇销量“六连跌”,不再安全的沃尔沃还能翻身吗?

How strong is this glue?
![[filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code](/img/e5/878bf5908856c80d6e07b03ba59da5.png)
[filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code

当初的“你“为什么做测试/开发程序员?自己存在的价值......
![[C language] implementation of three piece chess games](/img/53/7ee14e604c06fd77d65af29d6d92b8.png)
[C language] implementation of three piece chess games

Mycms we media mall V3.6 release, compatible with micro engine application development (laravel framework)

Submission records of frontiers Publishing House (with status changes)
随机推荐
18张图,直观理解神经网络、流形和拓扑
High quality programming
Assembly analysis swift polymorphism principle
Messages from students participating in the competition: memories of the 17th session
A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network
Kotlin JVM annotation
The Gerrit local code is associated to the remote warehouse
一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
【滤波跟踪】基于EKF、时差和频差定位实现目标跟踪附matlab代码
《MySQL数据库进阶实战》读后感(SQL 小虚竹)
There are four ways for Nacos to configure hot updates and multiple ways to read project configuration files, @value, @refreshscope, @nacosconfigurationproperties
IOS interview
华为无线设备配置利用WDS技术部署WLAN业务
Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
cnpm安装步骤
【C语言】三子棋小游戏实现
xshell7,xftp7个人免费版官方下载,无需破解,免激活,下载即可使用
Typescript防止基类被实例化
[database]
Typescript类方法this指针绑定