当前位置:网站首页>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());边栏推荐
- Nacos配置热更新的4种方式、读取项目配置文件的多种方式,@value,@RefreshScope,@NacosConfigurationProperties
- Sdwebimage source code comb 4 # introduce several usages of existing code
- Solve the exception that all control files are damaged
- [database]
- Subscript in swift
- All aspect visual monitoring of istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
- Hands on Teaching of servlet use (1)
- A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network
- Thesis reading (2) - vggnet of classification
- 华为无线设备配置利用WDS技术部署WLAN业务
猜你喜欢

一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力

xshell7,xftp7个人免费版官方下载,无需破解,免激活,下载即可使用

Thesis reading (1) - zfnet of classification

Submission records of frontiers Publishing House (with status changes)

sql优化常用的几种方法

RouYi-Cloud平台 ---项目的启动、登录功能是怎么实现的、怎么样创建新模块
![[MySQL series] addition, deletion, modification and query of MySQL tables (Advanced)](/img/4c/ba2fcb6e9be2f16b73e60ab84e3f3f.png)
[MySQL series] addition, deletion, modification and query of MySQL tables (Advanced)

Messages from students participating in the competition: memories of the 17th session

18 diagrams, intuitive understanding of neural networks, manifolds and topologies
![[radar] radar signal online sorting based on kernel clustering with matlab code](/img/56/1f8e8690b47fc4a1f101d4e530b87f.png)
[radar] radar signal online sorting based on kernel clustering with matlab code
随机推荐
【C语言】三子棋小游戏实现
以价换量却遭遇销量“六连跌”,不再安全的沃尔沃还能翻身吗?
Solve the exception that all control files are damaged
[filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code
Sdwebimage source code combs 5 author motivation, modifies directory, and changes inheritance relationship
Solve the problem of using anonymous users in pod due to the failure of attaching ciphertext token files for serviceaccount user authentication
这个胶水有多强呢?
以流量为主导的数字零售的发展模式,仅仅只是一个开始
View APK signature
Programmer growth Chapter 30: artifact of identifying true and false needs
6 个超级良心的开源教程!
VR全景创业如何开拓市场?如何让创业之路更加顺畅?
英特尔数据中心GPU正式发货,以开放灵活提供强劲算力
Huawei wireless device configuration uses WDS technology to deploy WLAN services
Recurrent neural network (RNN)
安全狗入选《云安全全景图2.0》多个细项
Sqlilabs-1 (breakthrough record)
Summary of core functions of software testing tool Fiddler postman JMeter charlse
c语言进阶篇:指针(三)
c语言进阶篇:指针(二)