孤君无依 2022-05-04 13:56:35
- 使用max-width优先级高于width特性
- 使用transform的scale
- 使用js拿到element后操作
孤君无依 2022-05-04 13:51:21
写个不用递归用循环的方式实现的版本吧
const getType = obj => Object.prototype.toString.call(obj).match(/[objects(.*)]/)[1] function deepClone(obj) { let res = {} let stack = [] let root = { parent: obj, prop: null, data: res } let wm = new WeakMap() stack.push(root) while (stack.length) { let item = stack.pop() Reflect.ownKeys(item.parent).forEach(key => { if (wm.get(item.parent[key])) { item.data[key] = wm.get(item.parent[key]) return } switch (getType(item.parent[key])) { case 'Object': { item.data[key] = {} stack.push({ parent: item.parent[key], prop: key, data: item.data[key] }) wm.set(item.parent[key], item.parent[key]) break } case 'Array': { item.data[key] = [] stack.push({ parent: item.parent[key], prop: key, data: item.data[key] }) wm.set(item.parent[key], item.parent[key]) break } case 'Date': { item.data[key] = new Date(item.parent[key]) break } case 'RegExp': { item.data[key] = new RegExp(item.parent[key]) break } default: { item.data[key] = item.parent[key] } } }) } return res } let obj = { num: 0, str: '', boolean: true, unf: undefined, nul: null, obj: { name: '我是一个对象', id: 1, qwe: { a: 1 } }, arr: [0, 1, 2, {b: 2}], date: new Date(0), reg: /我是一个正则/ig, [Symbol('1')]: 1, func() { console.log(123) } }; obj.loop = obj let cloneObj = deepClone(obj); console.log('obj', obj); console.log('cloneObj', cloneObj); // 对比两个对象引用类型的值是相同 Object.keys(cloneObj).filter(key => key !== 'nul').forEach(key => { if (typeof cloneObj[key] === 'object' || typeof cloneObj[key] === 'function') { console.log(`${key}相同吗? `, cloneObj[key] === obj[key]) } })
函数拷贝不了,还有一些奇奇怪怪的引用类型也拷贝不了,一般情况应该没啥问题,其实拷贝函数有一种思路是用AST(手动狗头)
- 共 1 页
- 1
你这样的写法a = a + c已经在一个微任务队列中,这时候由于同步任务a++先执行,导致a被读取时为1,你的写法等价于
第 12 题:JS 异步解决方案的发展历程以及优缺点