当前位置:网站首页>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());边栏推荐
- 【MySQL系列】 MySQL表的增删改查(进阶)
- Win11找不到DNS地址怎么办?Win11找不到DNS无法访问网页解决方法
- High quality subroutine 1
- Elements in the middle (one article is enough)
- 【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
- GCD summary
- Sqlilabs-2 (breakthrough record)
- A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network
- 英特尔数据中心GPU正式发货,以开放灵活提供强劲算力
- Seagate released a new risc-v architecture processor: the performance of mechanical hard disk soared 3 times
猜你喜欢

6 open source tutorials of super conscience!

MySQL数据库的基本概念以及MySQL8.0版本的部署(一)

深开鸿:万物智联的大江上,升起一轮开源鸿蒙月

Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)

当初的“你“为什么做测试/开发程序员?自己存在的价值......

Assembly analysis swift polymorphism principle

业界首创云原生安全检测双模型!安全狗重磅报告亮相数字中国建设峰会
![[physical application] atmospheric absorption loss with matlab code](/img/72/e6ac23012a59ac48a37bcbb068890b.png)
[physical application] atmospheric absorption loss with matlab code

The industry's first cloud native security detection dual model! Safety dog heavyweight report appears at the digital China Construction Summit

Messages from students participating in the competition: memories of the 17th session
随机推荐
【MySQL系列】 MySQL表的增删改查(进阶)
Terminal output G_ Debug() information
In 2020, the top ten domestic IC design enterprises will be exposed! These five industrial challenges still need to be overcome!
Performance optimized APK slimming
c语言进阶篇:指针(三)
High quality subroutine 1
Media query adaptation
Huawei wireless device configuration uses WDS technology to deploy WLAN services
Research on cookies in WebView
Pgbench benchmark PostgreSQL
In order for digital retail to continue to play its role, we need to give new connotation and significance to digital retail
Several common methods of SQL optimization
Win11找不到DNS地址怎么办?Win11找不到DNS无法访问网页解决方法
Text is hidden beyond and ellipsis is displayed
RouterOS 有限dns劫持及check
WebApplicationType#deduceFromClasspath
【雷达】基于核聚类实现雷达信号在线分选附matlab代码
行泊一体迎爆发期,抢量产还是修技术护城河?
Advanced C language: pointer (2)
It's settled! All products of Nezha s will be launched on July 31