当前位置:网站首页>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
}
}边栏推荐
猜你喜欢

原生高性能抓包工具Proxyman,送给爱学习的你

哈夫曼编码原理

Realize the full link grayscale based on Apache APIs IX through MSE

Quick sort
![Rust语言- Slice(切片)类型(&[u8])](/img/d1/68c73c8b34b848212083c08df3137f.png)
Rust语言- Slice(切片)类型(&[u8])

String and memory functions

TCP protocol -- message format, connection establishment, reliable transmission, congestion control
![Rust language - slice type (&[u8])](/img/d1/68c73c8b34b848212083c08df3137f.png)
Rust language - slice type (&[u8])

XSS-labs(1-10)闯关详解

AcWing-每日一题
随机推荐
Force buckle - 3. Longest substring without repeated characters
mysql优化之show profile的使用及分析
Children's programming electronic society graphical programming level examination scratch level 1 real problem analysis (multiple choice) June 2022
【Star项目】小帽飞机大战(二)
CS5801_HDMI转EDP优势替代LT6711A方案
少儿编程 电子学会图形化编程等级考试Scratch一级真题解析(选择题)2022年6月
【硬十宝典】——7.2【动态RAM】DDR4与DDR3区别解析
日志轮转logrotate
28. Implement strStr()实现 strStr()
Delete ^m from VIM
SQL optimization scheme
Conda 虚拟环境envs目录为空
Which "digital currencies" can survive this winter? 2020-03-30
MySQL optimized index and index invalidation
Realize the full link grayscale based on Apache APIs IX through MSE
归并排序(merge_sort)
『HarmonyOS』DevEco的下载安装与开发环境搭建
【图像去噪】基于双立方插值和稀疏表示实现图像去噪matlab源码
Facing the rebound market, how do we operate? 2020-03-21
[Star Project] small hat aircraft War (IV)