function deepCopy(target, hash = new WeakMap()) {
const type = Object.prototype.toString.call(target).slice(8, -1)
let res
if (type === 'Object') res = {}
else if (type === 'Array') res = []
else return target
if (hash.has(target)) return hash.get(target)
hash.set(target, res)
for (let key in target) {
res[key] = deepCopy(target[key], hash)
}
return res
}
// const obj = {
// foo: {
// azz: [1, 3, 2]
// },
// age: [1, {
// baz: 71
// }],
// num: 3
// }
// let objc = deepCopy(obj);
// console.log(obj)
// console.log(objc)
// console.log(objc === obj)
function myall(iterator) {
return new Promise((resolve, reject) => {
let reslist = []
let i = 0
let len = 0
for (const p of iterator) {
let resIndex = len++
Promise.resolve(p).then((res) => {
console.log(`${resIndex}: ${res}`);
reslist[resIndex] = res
if (++i === len) resolve(reslist)
}).catch(reject)
}
if (len === 0) resolve(reslist)
})
}
// let arr = [];
// let index = 0;
// while (index < 5) {
// index += 1;
// arr.push(
// new Promise((resolve, reject) => {
// let num = Math.floor(Math.random() * 5 + 1);
// setTimeout(() => {
// resolve(`${num}s`);
// // num > 5 ? resolve(num) : reject(num);
// }, num * 1000);
// })
// );
// }
// myall(arr).then((res) => console.log("res", res)).catch((err) => console.log("err", err));
function asyncPool(poolLimit, urls, request) {
let ret = [], executing = [], i = 0
function enqueue() {
if (i >= urls.length) return Promise.resolve()
const p = Promise.resolve(request(urls[i++]))
ret.push(p)
executing.push(p)
p.then(() => executing.splice(executing.indexOf(p), 1))
let r = Promise.resolve()
if (executing.length >= poolLimit) r = Promise.race(executing)
return r.then(() => enqueue())
}
return enqueue().then(() => Promise.all(ret))
}
// let arr = [];
// let index = 0;
// while (index < 10) {
// index++;
// arr.push(index);
// }
// asyncPool(3, arr, (param) => {
// return new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve(param);
// }, Math.floor(Math.random() * 5 + 1) * 1000);
// });
// })
// .then((res) => console.log("res", res));
function sort(nums) {
if (nums.length <= 1) return nums
let left = [], right = []
let middleIdx = Math.floor(nums.length / 2)
let middle = nums.splice(middleIdx, 1)[0]
nums.forEach(item => {
if (item < middle) left.push(item)
else right.push(item)
})
return sort(left).concat(middle, sort(right))
}
// console.log(sort([5, 1, 3, 4, 2]))
var removeDuplicates = function (nums) {
let res = []
nums.forEach(item => {
if (res.indexOf(item) === -1) res.push(item)
})
return res
};
// console.log(removeDuplicates([5, 5, 6, 7, 6, 1, 1, 3, 4, 2]))
function search(nums, target) {
let left = 0, right = nums.length - 1
while (left <= right) {
let middle = Math.floor(left + (right - left) / 2)
if (nums[middle] === target) return middle
let isSmall = nums[middle] < target
left = isSmall ? middle + 1 : left
right = isSmall ? right : middle - 1
}
return -1
}
// console.log(search([-1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 6))
function Parent() {
this.hobby = ['跑步']
}
Parent.prototype.showHobby = function () {
console.log(this.hobby)
}
function Child(name) {
this.name = name
Parent.call(this)
}
Child.prototype = new Parent()
Child.prototype.constructor = Child
Child.prototype.showName = function () {
console.log(this.name)
}
function myNew(fn, ...args) {
const obj = Object.create(fn.prototype)
const ret = fn.apply(obj, args)
return ret instanceof Object ? ret : obj
}
// let c = new Child('zhang san')
// let d = myNew(Child, 'zhang san')
// console.log(c.__proto__)
// console.log(d.__proto__)
// c.showHobby()
// d.showHobby()
// c.showName()
// d.showName()
console