install
source · Clone the upstream repo
git clone https://github.com/majiayu000/claude-skill-registry
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/majiayu000/claude-skill-registry "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/data/javascript" ~/.claude/skills/majiayu000-claude-skill-registry-javascript && rm -rf "$T"
manifest:
skills/data/javascript/SKILL.mdsource content
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
提前终止操作吗?returnfunction 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
:与逻辑或操作符不同,逻辑或操作符会在左侧操作数为假值时返回右侧操作数。也就是说,如果使用||
来为某些变量设置默认值,可能会遇到意料之外的行为。比如为假值(例如,||
或''
)时。见下面的例子0const 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
★★★★★★★★★★