实现浅拷贝
let test1 = 1
let test2 = { name: 'lisi' }
let test1_1 = test1
let test2_1 = test2
test1_1 = 2
test2_1.name = 'zhangsan'
console.log(test1)
console.log(test1_1)
console.log(test2)
console.log(test2_1)
let target = { a: 1 }
let object1 = { b: 2 }
let object2 = { c: 3 }
Object.assign(target, object1, object2)
console.log(target)
let target = { a: 1 }
let object1 = { a: 2 }
let object2 = { a: 4, c: 3 }
Object.assign(target, object1, object2)
console.log(target)
let target = { a: 1 }
Object.assign(target)
console.log(target)
console.log(typeof target)
let target = 111
Object.assign(target)
console.log(target)
console.log(typeof target)
let obj1 = {
a: 1,
b: {
c: 1
}
}
let obj2 = {...obj1}
obj2.a = 2
obj2.b.c = 2
console.log(obj1)
console.log(obj2)
let arr = [1,2,3,4]
let copy = arr.slice()
console.log(copy)
console.log(copy === arr)
let arr = [1, true, null, { name: 'lisi'}]
let copy = arr.slice()
console.log(arr)
console.log(copy)
copy[3].name = 'zhangsan'
console.log(arr)
console.log(copy)
let arr = [1,2,3,4]
let copy = arr.concat()
console.log(copy)
console.log(arr === copy)
let arr = [1, true, null, { name: 'lisi' }]
let copy = arr.concat()
console.log(arr)
console.log(copy)
copy[3].name = 'zhangsan'
console.log(arr)
console.log(copy)
function shallowCopy(object) {
if (!object || typeof object !== 'object') {
return console.error('只拷贝对象类型的数据')
}
let newObject = Array.isArray(object) ? [] : {}
for (let key in object) {
console.log('key=', key)
if (object.hasOwnProperty(key)) {
newObject[key] = object[key]
}
}
return newObject
}
let obj = {
a: 1,
b: 2,
c: {
d: 3
}
}
let copy = shallowCopy(obj)
console.log(copy)
copy.a = 10
copy.c.d = 30
console.log(obj)
console.log(copy)
let arr = [1, 2, { name: 'lisi' }]
let copy = shallowCopy(arr)
console.log(copy)
copy[0] = 10
copy[2].name = 'zhangsan'
console.log(arr)
console.log(copy)
let obj = Object.create({ name: 'jicheng'} )
obj.a = 1
const copy = shallowCopy(obj)
console.log(copy)
实现深拷贝
let obj = {
a:0,
b: {
c: 1
}
}
let copy = JSON.parse(JSON.stringify(obj))
console.log(copy)
copy.b.c = 100
console.log(obj)
console.log(copy)
let obj = {
a: function() {
console.log(111)
},
b: undefined,
c: Symbol(),
d: {
e: 1
},
f: 111,
g: null
}
let copy = JSON.parse(JSON.stringify(obj))
console.log(copy)
let _ = require('lodash')
let obj = {
a:1,
b: null,
c: undefined,
d: function() {
console.log(222)
},
e: Symbol,
f: {
h: {
i: 100
}
}
}
let copy = _.cloneDeep(obj)
console.log(obj.f.h === copy.f.h)
function deepCopy(value, map = new WeakMap()) {
if (!value || typeof value !== 'object') {
return value
}
if (map.has(value)) {
return map.get(value)
}
if (value instanceof Date) {
return new Date(value)
}
if (value instanceof RegExp) {
return new RegExp()
}
if (Array.isArray(value)) {
let arrCopy = []
map.set(value, arrCopy)
for (let i = 0; i < value.length; i++) {
arrCopy[i] = deepCopy(value[i], map)
}
return arrCopy
}
let objCopy = {}
map.set(value, objCopy)
for (let key in value) {
if (value.hasOwnProperty(key)) {
objCopy[key] = deepCopy(value[key], map)
}
}
return objCopy
}
let obj = {
name: 'John',
age: 30,
circular: null
}
obj.circular = obj
let copy = deepCopy(obj)
console.log(copy)
console.log(copy.circular === copy)
console.log(copy.circular === obj)
let obj = {
a: 1,
b: null,
c: undefined,
d: function () {
console.log(222)
},
e: Symbol(1),
f: {
h: {
i: 100
}
}
}
let copy = deepCopy(obj)
function test(value) {
return value
}
let a = Symbol(1)
let b = test(a)
console.log(b)
console.log(a === b)
实现sleep函数
function timeout(delay) {
return new Promise(resolve => {
setTimeout(resolve, delay)
})
}
(async function(){
console.log('开始')
await timeout(2000).then(() => {
console.log('2s后执行了')
})
console.log(111)
})()
手写Object.assign
Object.prototype.myAssign = function(target, ...sources) {
if(!target) {
throw new TypeError('Connot convert undefined or null to object')
}
let tar = Object(target)
sources.forEach(source => {
for(let key in source) {
if(source.hasOwnProperty(key)) {
tar[key] = source[key]
}
}
})
return tar
}
let target = {
a: 1
}
let source1 = {
b:2
}
let source2 = {
a: 100,
c:2
}
let res = Object.myAssign(target, source1, source2)
console.log(res)
console.log(target)