这节讲的太糙了,根本没有讲出闭包的精髓,而且例子举的也不好,ES6已经出来这么久了,除了框架和底层工具编写者,没人会使用var声明变量了。闭包的应用有很多,比如封装私有变量,模拟模块,防抖节流柯里化,单例模式等等。
3天前
判断typeof olddata[key] === 'object'要注意要先判断olddata[key]不是null才行!否则程序会报错!!!
if (olddata[key]&&typeof olddata[key] === 'object') {
obj[key] = olddata[key].constructor === Array ? [] : {}
this.deepCopy(olddata[key], obj[key])
} else {
obj[key] = olddata[key]
}
if (olddata[key]&&typeof olddata[key] === 'object') {
obj[key] = olddata[key].constructor === Array ? [] : {}
this.deepCopy(olddata[key], obj[key])
} else {
obj[key] = olddata[key]
}
2022-07-18
var twoSum = function(nums, target) {
const map = new Map()
for (let i = 0; i < nums.length; i++) {
const num = nums[i]
let o =target -num
if (map.has(o)) return [i,map.get(o)]
map.set(num, i)
}
};
const map = new Map()
for (let i = 0; i < nums.length; i++) {
const num = nums[i]
let o =target -num
if (map.has(o)) return [i,map.get(o)]
map.set(num, i)
}
};
2022-04-29
function deepClone(params) {
const initData = params instanceof Array ?[]:{}
for (const i in params) {
initData[i]= params[i] instanceof Object ? deepClone(params[i]):params[i]
}
return initData
}
const initData = params instanceof Array ?[]:{}
for (const i in params) {
initData[i]= params[i] instanceof Object ? deepClone(params[i]):params[i]
}
return initData
}
2022-04-29