這節(jié)講的太糙了,根本沒有講出閉包的精髓,而且例子舉的也不好,ES6已經(jīng)出來這么久了,除了框架和底層工具編寫者,沒人會使用var聲明變量了。閉包的應用有很多,比如封裝私有變量,模擬模塊,防抖節(jié)流柯里化,單例模式等等。
2024-11-20
判斷typeof olddata[key] === 'object'要注意要先判斷olddata[key]不是null才行!否則程序會報錯?。。?br />
if (olddata[key]&&typeof olddata[key] === 'object') {
obj[key] = olddata[key].constructor === Array ? [] : {}
this.deepCopy(olddata[key], obj[key])
} else {
obj[key] = olddata[key]
}
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