JavaScript 小技巧 笔记
★★★★★★★★★★
-
多次解构
let obj = { part1: { name: '01', age: 23 } } // 解构 const { part1: { name, age } } = obj // 使用 console.log(name, age) // "01" 23 console.log(part1) // Uncaught ReferenceError: part1 is not defined // 多次解构 const { part1: { name, age }, part1 } = obj console.log(part1) // {name: "01", age: 23} -
数字分隔符
const myMoney = 1_000_000_000_000 console.log(myMoney) // 1000000000000 -
try|catch|finally能使用return提前终止操作吗?function demo() { try { return 1 } catch (err) { console.log(err) return 2 } finally { try { return 3 } finally { return 4 } } } console.log(demo()) // 4 -
获取当前调用栈
function firstFunction() { secondFunction(); } function secondFunction() { thridFunction(); } function thridFunction() { console.log(new Error().stack); } firstFunction(); //=> Error // at thridFunction (<anonymous>:2:17) // at secondFunction (<anonymous>:5:5) // at firstFunction (<anonymous>:8:5) // at <anonymous>:10:1使用
new Error().stack就能随时获取到当前代码执行的调用栈信息,也是一种调试代码的办法 -
如何快速生成随机数字+字符串
const str = Math.random().toString(36).substr(2, 10); console.log(str); // 'w5jetivt7e'先是
Math.random()生成[0, 1)的数,然后调用number的toString方法将其转换成36进制的,按照MDN的说法,36进制的转换应该是包含了字母a~z和 数字0~9的,因为这样生成的是0.89kjna21sa类似这样的,所以要截取一下小数部分,即从索引2开始截取10个字符就是我们想要的随机字符串了 -
最快获取
dom的方法<div id="zero2one"></div> const el = document.getElementById('zero2one') console.log(el) // <div id="zero2one"></div> // 等同于 console.log(zero2one) // <div id="zero2one"></div> -
||和??的区别???:空值合并操作符是一个逻辑操作符,当左侧的操作数为null或者undefined时,返回其右侧操作数,否则返回左侧操作数。||:与逻辑或操作符不同,逻辑或操作符会在左侧操作数为假值时返回右侧操作数。也就是说,如果使用||来为某些变量设置默认值,可能会遇到意料之外的行为。比如为假值(例如,''或0)时。见下面的例子const foo = null ?? 'default string'; console.log(foo); // expected output: "default string" const baz = 0 ?? 42; console.log(baz); // expected output: 0 -
Array.prototype.sort()中排序顺序arr.sort():默认从小到大arr.sort((a, b) => a - b):从小到大arr.sort((a, b) => b - a):从大到小 -
在使用
EventSource的过程中注意:eventSource.onmessage:只能监听默认的type = message的事件eventSource.addEventListener("custom", (event) => {}):可以监听其它custom事件
★★★★★★★★★★