当前位置:网站首页>vant 源码解析 之深层 合并对象 深拷贝
vant 源码解析 之深层 合并对象 深拷贝
2022-07-05 20:58:00 【丰的传说】
源码
将两对象的属性合并,深层对象也可以
function isDef(value: any): boolean {
// value:any 表示传入的参数是任意类型 boolean 表示返回值是boolean类型
return value !== undefined && value !== null;
// 传入的值不能是undefined 也不能是null
}
// in 15.686s 0.486s
function isObj(x: any): boolean {
debugger
//判断一个值是否是 对象类型
// 传入的参数 是 any 任意类型i
// 返回值是 boolean
const type = typeof x;
// typeof 判断数据类型
// typeof 判断数据类型时 null 也是 object 所以 type 不能为null
// 函数也是 对象
return x !== null && (type === 'object' || type === 'function');
}
const { hasOwnProperty } = Object.prototype;
//判断对象是否包含某个属性(不包含对象原型上的属性)
type objectType = {
[key: string]: any;
}
function assignKey(to: objectType, from: objectType, key: string) {
const val = from[key];
// 拿到老的数据的 某个key的值
if (!isDef(val)) {// 当前key对应的 老数据的值 是undefined就return
return;
}
if (!hasOwnProperty.call(to, key) || !isObj(val)) {
// 如果目标对象的 这个key 不存在就 老数据对应的key值 赋值给 目标对象to
to[key] = val;
} else {
to[key] = deepAssign(Object(to[key]), from[key]);
}
}
function deepAssign(to: objectType, from: objectType) {
Object.keys(from).forEach(key => {
assignKey(to, from, key);
});
return to;
}
测试
let toInfo={
name:"123",
age:'16',
more:{
hobby:'打篮球',
address:'合肥'
},
color:'red'
}
let fromInfo={
name:"12343553",
age:'16545353',
more:{
hobby:'打篮球5353',
address:'合肥5353'
},
sex:'男'
}
let result: objectType = deepAssign(toInfo,fromInfo)
console.log("result",result)
深拷贝
import { deepAssign } from './deep-assign';
export function deepClone(obj: object): object {
if (Array.isArray(obj)) {
return obj.map(item => deepClone(item));
}
if (typeof obj === 'object') {
return deepAssign({}, obj);
}
return obj;
}
边栏推荐
- 模式-“里氏替换原则”
- Norgen AAV extractant box instructions (including features)
- Duchefa MS medium contains vitamin instructions
- Simple getting started example of Web Service
- Enclosed please find. Net Maui's latest learning resources
- haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
- 从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
- Write an interface based on flask
- Careercup its 1.8 serial shift includes problems
- AITM2-0002 12s或60s垂直燃烧试验
猜你喜欢
研學旅遊實踐教育的開展助力文旅產業發展
重上吹麻滩——段芝堂创始人翟立冬游记
Prosci LAG-3 recombinant protein specification
Abnova maxpab mouse derived polyclonal antibody solution
MySQL InnoDB架构原理
如何让化工企业的ERP库存账目更准确
CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
基于flask写一个接口
渗透创客精神文化转化的创客教育
Which is the best online collaboration product? Microsoft loop, notion, flowus
随机推荐
Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
清除app data以及获取图标
matplotlib绘图润色(如何形成高质量的图,例如设如何置字体等)
systemd-resolved 开启 debug 日志
Sophomore personal development summary
示波器探头对测量带宽的影响
Abnova e (diii) (WNV) recombinant protein Chinese and English instructions
Learning notes of SAS programming and data mining business case 19
How to renew NPDP? Here comes the operation guide!
渗透创客精神文化转化的创客教育
序列联配Sequence Alignment
基于flask写一个接口
基于AVFoundation实现视频录制的两种方式
Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
【案例】定位的运用-淘宝轮播图
Write an interface based on flask
《SAS编程和数据挖掘商业案例》学习笔记# 19
Talk about my fate with some programming languages
LeetCode: Distinct Subsequences [115]
AITM 2-0003 水平燃烧试验