// 实现简单的深拷贝
function cloneDeep(obj) {
let newObj = {}
for(let key in obj) {
if (typeof key === 'object') {
newObj[key] = cloneDeep(obj[key])
} else {
newObj[key] = obj[key]
}
}
return newObj
}
> 箭头函数
```js
let sum21 = function(a, b) {
return a + b
}
// 简化1
let sum31 = (a, b) => {
return a + b
}
// 简化2
let sum22 = (a, b) => a + b
// 简化3 如果只有一个参数
let test = b => b
let obj = {
name1: 'zhuyaogeng',
getName() {
setTimeout(() => {
console.log(this.name1, 'name1')
}, 100)
}
}
obj.getName()
函数
}
封装自己的reduce方法
// 实现reduce方法 Array.prototype.reduce1 = function(reducer, initialval) { for (let i = 0; i < this.length; i++ ) { // 递归 initialval = reducer(initialval, this[i]) } return initialval }
const result2 = [1,2, 4].reduce1(function(val, item) { return val + item }, 0)
console.log(result2)
let arr4 = [1, 2, 3] let arr5 = [4, 5, 6] let arr6 = [].concat(arr4).concat(arr5) // 展开运算符,相当于把数组的每个元素依次取出放出 let arr7 = [...arr4, ...arr5] console.log(arr6) console.log(arr7)
// 拿出最大值 let max = Math.max(...arr7) console.log(max)
let obj1 = { name: 1 } let obj2 = { age: 22 } let obj3 = {}
// 实现简单的深拷贝 function cloneDeep(obj) { let newObj = {} for(let key in obj) { if (typeof key === 'object') { newObj[key] = cloneDeep(obj[key]) } else { newObj[key] = obj[key] } }
}