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

Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral

王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动

Which is the best online collaboration product? Microsoft loop, notion, flowus

CLion配置visual studio(msvc)和JOM多核编译

Abnova丨CRISPR SpCas9 多克隆抗体方案

Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang

Abnova blood total nucleic acid purification kit pre installed relevant instructions

Duchefa MS medium contains vitamin instructions

leetcode:1755. 最接近目标值的子序列和

Abnova丨荧光染料 620-M 链霉亲和素方案
随机推荐
台风来袭!建筑工地该如何防范台风!
POJ 3414 pots (bfs+ clues)
Write an interface based on flask
AITM2-0002 12s或60s垂直燃烧试验
CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
Abnova maxpab mouse derived polyclonal antibody solution
Duchefa p1001 plant agar Chinese and English instructions
示波器探头对信号源阻抗的影响
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
珍爱网微服务底层框架演进从开源组件封装到自研
systemd-resolved 开启 debug 日志
实现浏览页面时校验用户是否已经完成登录的功能
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
shell编程100例
leetcode:1755. 最接近目标值的子序列和
最长摆动序列[贪心练习]
ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
When steam education enters personalized information technology courses
Learning notes of SAS programming and data mining business case 19