当前位置:网站首页>Depth cloning and reflection of typescript class objects
Depth cloning and reflection of typescript class objects
2022-07-26 06:51:00 【RemoteDev】
1. Define base and derived classes
// Define base class
/*
* class
* */
class myClass{
/*
* Constructors
* */
constructor(x,y) {
// @ts-ignore
Object.assign(this,{x,y});// Add attribute
}
}
// Subclasses continue to base classes
class mySubClass extends myClass{
// structure
constructor() {
super(200,300);
}
// Class instance method
hello(){
console.log('hello,mySubClass');
}
}2. Object depth cloning method implementation
// Do not continue cloning --- Shallow clone
let clone=(obj)=>{
// @ts-ignore
return Object.assign({},obj);
}
// Inheritance cloning ---- A deep clone
let clonedeep=(obj)=>{
let objProto = Object.getPrototypeOf(obj);
// @ts-ignore
return Object.assign(Object.create(objProto),obj);
}3. Instantiate class objects and clone
// Instantiate the derived class and call the instance method hello
let mysub = new mySubClass();
console.log(mysub,mysub.x,mysub.y);
mysub.hello();
// Class object cloning
let subClone = clone(mysub); // Shallow clone
console.log(subClone);
//subClone.hello();// Shallow clone hello Method is not cloned
let subDeepClone = clonedeep(mysub);// A deep clone
console.log(subDeepClone);
subDeepClone.hello();// A deep clone hello Methods can be cloned
4. Call class instance methods through reflection
// @ts-ignore
console.log( Reflect.has(subClone,'hello'));//false There is no way
// @ts-ignore
console.log( Reflect.has(subDeepClone,'hello'));//true Yes hello Method Next, first confirm that the object has hello Method , And then through Reflect.get Get the method and call
// @ts-ignore
if(Reflect.has(subDeepClone,'hello')){ // If the object exists hello This property name
// @ts-ignore
let func_hello = Reflect.get(subDeepClone,'hello');
// @ts-ignore
if(typeof (func_hello) == 'function'){ // If hello Is the function
console.log(' Execute functions through reflection ===');
func_hello();// Execute function
}
}边栏推荐
- Experimental flags: --disable_ admission_ control=false --enable_ rm=false --llama_ callback_ port=28000
- MySQL table read lock
- C#使用log4net插件,输出日志到文件
- Overview of image classification of vision transformer must read series
- UIToolkit工具模板工程
- Torth file read vulnerability (cnvd-2020-27769)
- Merge_sort
- On stock price prediction model (3): are you falling into the trap of machine learning
- [image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code
- Download, installation and development environment construction of "harmonyos" deveco
猜你喜欢

Huffman coding principle

vulnhub Lampião: 1

MySql 执行计划
![Rust language - slice type (&[u8])](/img/d1/68c73c8b34b848212083c08df3137f.png)
Rust language - slice type (&[u8])

TCP protocol -- message format, connection establishment, reliable transmission, congestion control

Why the server is stuck

Click "Niuke | daily question" to eliminate it

MySQL基础篇(二)-- MySQL 基础
![[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code](/img/2a/b5214e9fa206f1872293c9b9d7bdb6.png)
[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code

『牛客|每日一题』逆波兰表达式
随机推荐
Display Chinese characters in uitoolkit
Regular expressions and calling related functions in C language
7. Reverse Integer整数反转
MySQL基础篇(二)-- MySQL 基础
MySQL optimized index and index invalidation
7. Reverse integer integer
【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码
On the difference between Eval and assert
PMP customs formula, don't hurry to recite it
信号处理系统综合设计-求解器函数的设计(连续和离散时间系统)
28. Implement strstr() implement strstr()
<二> objectARX开发:创建和编辑基本图形对象
哈夫曼编码原理
Realize the full link grayscale based on Apache APIs IX through MSE
Celery takes up large memory - memory leak
Is the passenger flow always low? There is something wrong with the location of your store!
vulnhub Lampião: 1
抖音web端 s_v_web_id 参数生成分析与实现
[graduation season _ advanced technology Er] farewell to yourself who has been confused for the past two years. Regroup, junior I'm coming
123123123