当前位置:网站首页>深浅拷贝
深浅拷贝
2022-07-21 23:05:00 【InfoQ】
深浅拷贝
// 1. 赋值
var o1 = {
name: 'Jason'
}
var o2 = o1
2. 浅拷贝
var o1 = {
name: 'Jack',
age: 18,
info: {
class: 'gp20',
score: 100
}
}
var o2 = {}
for (var key in o1) {
o2[key] = o1[key]
}
o2.name = 'Rose'
o2.info.score = '90'
console.log(o1, o2)
方案1: 利用递归思想实现深拷贝
方案2: json
// 3. 深拷贝
var o1 = {
name: 'Jack',
age: 18,
info: {
class: 'gp20',
scroe: 100,
time: {
createTime: 100,
updateTime: 200,
times: [1, 2, 3, 4, 5]
}
}
}
var o2 = {}
// 第一遍遍历
for (var key in o1) {
if (typeof o1[key] === 'object') {
if (o1[key].constructor === Array) {
o2[key] = []
} else {
o2[key] = {}
}
for (var key2 in o1[key]) {
o2[key][key2] = o1[key][key2]
}
} else {
o2[key] = o1[key]
}
}
function deep(o2, o1) {
for (var key in o1) {
if (o1[key].constructor === Array) {
o2[key] = []
deep(o2[key], o1[key])
} else if (o1[key].constructor === Object) {
o2[key] = {}
deep(o2[key], o1[key])
} else {
o2[key] = o1[key]
}
}
}
deep(o2, o1)
// o2 = JSON.parse(JSON.stringify(o1))
o2.name = 'Rose'
o2.info.scroe = 90
o2.info.time.createTime = 300
o2.info.time.times[100] = 99
console.log(o1, o2)
jQuery 里面的深浅拷贝
var o1 = {
name: '我是 o1'
}
var o2 = {
age: 18,
info: {
class: 'gp20'
}
}
var o3 = {
gender: '男',
list: [1, 2, 3, 4, 5]
}
// 进行深拷贝
$.extend(true, o1, o2)
o1.age = 20
o1.info.class = '你好'
console.log(o1, o2)
jQuery 的插件扩展
// 向 jQuery 身上扩展了一个 a 方法
$.extend({
getCookie (val) {
const cookie = document.cookie
var o = ''
if (val) {
cookie.split('; ').forEach(item => {
var t = item.split('=')
if (t[0] === val) {
o = t[1]
}
})
} else {
o = {}
cookie.split('; ').forEach(item => {
const t = item.split('=')
o[t[0]] = t[1]
})
}
return o
},
setCookie () {
}
})
const res = $.getCookie('a')
console.log(res)
console.log($.getCookie())
// 向 jQuery 的原型上扩展方法
$.fn.extend({
a: function () {
console.log('我调用了')
}
})
$.extend($.fn, {
b () {
console.log('我是 扩展的 b 方法')
}
})
// 使用的时候
$('html').a()
$('html').b()
边栏推荐
- ROS entry level tutorial
- Analysis of the advantages of the LAAS scheme of elephant swap led to strong performance of ETOKEN
- Error in invoking target 'agent nmhs' of makefile when installing Oracle 11g in red hat 4.8
- Supply chain collaborative management system of pharmaceutical machinery industry: full link digital coverage to realize the visualization of industrial supply chain
- leetcode 92. Reverse linked list II
- service(lb) 和管理的pod
- The LAAS solution of elephant swap has risen rapidly and built a new defi2.0 protocol
- [MySQL]索引事务
- ARM+SD2405 IIC_RTC驱动编写及IIC通讯协议
- Deep understanding of perfect hash
猜你喜欢
随机推荐
JVM parent delegation mechanism
ROS entry level tutorial
SQL statement. After grouping, the group is sorted according to the datetime field, the latest record in the group is retained, and other items are deleted. (used to eliminate duplicate data within th
同一个浏览器不同窗口登录不同账号,窗口切换时,页面刷新账号变更为最后一次登录的账号
JVM(9)之JVM对象创建与内存分配深度剖析
[MySQL]索引事务
mapState
Coordinate system in QT
Qt development application debug and release settings
Use the browser plug-in to run JS to delete the "disable copy" function of a specific website
Messy analysis view system
Error in invoking target 'agent nmhs' of makefile when installing Oracle 11g in red hat 4.8
How to select the type of ankerui intelligent miniature circuit breaker?
Deep understanding of perfect hash
Daily question-leetcode814-binary tree pruning recursion
"Xiaodeng in operation and maintenance" ensures gdpr compliance by auditing user activities and internal changes in the ad domain
工厂控制室西门子PLC如何集中无线采集多条产线生产数据?
制药机械行业供应链协同管理系统:全链路数字化覆盖,实现产业供应链可视化
【等保小知识】等保整改是什么意思?整改内容包括哪些?
What if Lao Xue's host disk space is full