当前位置:网站首页>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());边栏推荐
- Arduino框架下STM32F103C系列单片机引脚映射关系
- c语言进阶篇:指针(三)
- Inheritance in swift
- Xshell7, xftp7 personal free version official download, no need to crack, no activation, download and use
- Pgbench benchmark PostgreSQL
- [physical application] atmospheric absorption loss with matlab code
- Sdwebimage source code comb 4 # introduce several usages of existing code
- Elements in the middle (one article is enough)
- 【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
- Binary search tree
猜你喜欢

Recurrent neural network (RNN)
![[physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code](/img/31/e4cd4c261a7fc5cfa731976314530b.png)
[physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code

业界首创云原生安全检测双模型!安全狗重磅报告亮相数字中国建设峰会
Object object

Retrofit Usage Summary

【物理应用】大气吸收损耗附matlab代码

Sqlilabs-3 (entry notes)

It's settled! All products of Nezha s will be launched on July 31

华为无线设备配置利用WDS技术部署WLAN业务

Cglib create proxy
随机推荐
Invest 145billion euros! EU 17 countries announce joint development of semiconductor technology
Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
【C语言】三子棋小游戏实现
《MySQL数据库进阶实战》读后感(SQL 小虚竹)
Text is hidden beyond and ellipsis is displayed
The industry's first cloud native security detection dual model! Safety dog heavyweight report appears at the digital China Construction Summit
Summary of koltin knowledge points
No code development platform address book tutorial
Arduino UNO驱动合宙1.8‘TFT SPI屏幕示例演示(含资料包)
IOS interview
深开鸿:万物智联的大江上,升起一轮开源鸿蒙月
Win11找不到DNS地址怎么办?Win11找不到DNS无法访问网页解决方法
Sqlilabs-2 (breakthrough record)
Goer shares and Shanghai Taisi Weida growth cooperation agreement! Special SOC jointly promotes the development of TWS headphones
MyCms 自媒体商城 v3.6 发布,兼容微擎应用开发(Laravel框架)
Summary of core functions of software testing tool Fiddler postman JMeter charlse
Swift type attribute and its attentions
Win11快捷复制粘贴不能用怎么办?Win11快捷复制粘贴不能用
mgr.exe病毒导致启动程序启动失败
pg_ Installation and use of RMAN "PostgreSQL"