新聞中心
一、解構(gòu)小技巧
平常我們需要用到一個(gè)嵌套多層的對(duì)象中某些屬性,會(huì)將其解構(gòu)出來使用

- let obj = {
- part1: {
- name: '零一',
- age: 23
- }
- }
- // 解構(gòu)
- const { part1: { name, age } } = obj
- // 使用
- console.log(name, age) // 零一 23
這種情況下,你把 name 和 age 從 part1 里解構(gòu)出來了以后,你就無法使用變量 obj 中的 part1 屬性了,如:
- // .... 省略
- const { part1: { name, age } } = obj
- console.log(part1) // Uncaught ReferenceError: part1 is not defined
其實(shí)你可以多次解構(gòu),如:
- // .... 省略
- const { part1: { name, age }, part1 } = obj
- console.log(part1) // {name: "零一", age: 23}
二、數(shù)字分隔符
有時(shí)你會(huì)在文件中定義一個(gè)數(shù)字常量
- const myMoney = 1000000000000
這么多個(gè) 0 ,1、2 ... 6、7 ... 數(shù)暈了都,怎么辦?
數(shù)字分隔符整起來!
- const myMoney = 1_000_000_000_000
- console.log(myMoney) // 1000000000000
這樣寫是沒問題的,而且數(shù)字分割開后也更直觀??!
三、try...catch...finally 誰厲害?
普通函數(shù)調(diào)用中, return 一般會(huì)提前結(jié)束函數(shù)的執(zhí)行
- function demo () {
- return 1
- console.log('我是零一')
- return 2
- }
- console.log(demo()) // 1
而在 try...catch...finally 中, return 就不會(huì)提前結(jié)束執(zhí)行
- function demo () {
- try {
- return 1
- } catch (err) {
- console.log(err)
- return 2
- } finally {
- return 3
- }
- }
- console.log(demo()) // 返回什么??
這個(gè)程序會(huì)返回什么呢?思考一下
Tow hours Later~
答案是: 3
最后得出結(jié)論,還是 finally 比較厲害
那么我們可以搞一些騷操作
- function demo () {
- try {
- return 1
- } catch (err) {
- console.log(err)
- return 2
- } finally {
- try {
- return 3
- } finally {
- return 4
- }
- }
- }
- console.log(demo()) // 返回 4
四、獲取當(dāng)前調(diào)用棧
- function firstFunction() { secondFunction(); }
- function secondFunction() { thridFunction(); }
- function thridFunction() { console.log(new Error().stack); }
- firstFunction();
- //=> Error
- // at thridFunction (
:2:17) - // at secondFunction (
:5:5) - // at firstFunction (
:8:5) - // at
:10:1
new Error().stack 這樣就能隨時(shí)獲取到當(dāng)前代碼執(zhí)行的調(diào)用棧信息,也不失一種調(diào)試代碼的辦法
五、一行代碼生成隨機(jī)字符串
我最初學(xué)js時(shí),想自己實(shí)現(xiàn)一個(gè)隨機(jī)生成字符串的函數(shù),是這么搞的
- function hash () {
- let s = ''
- const strs = [
- 'a', 'b', 'c', 'd', 'e', 'f', 'g',
- 'h', 'i', 'j', 'k', 'l', 'm', 'n',
- 'o', 'p', 'q', 'r', 's', 't', 'u',
- 'v', 'w', 'x', 'y', 'z', '0', '1',
- '2', '3', '4', '5', '6', '7', '8',
- '9',
- ]
- for(let i = 0; i < 10; i++) {
- s += strs[Math.floor(Math.random() * strs.length)]
- }
- return s
- }
- hash() // 'www7v2if9r'
真麻煩?。∥夜鈱?6個(gè)字母和10個(gè)數(shù)字就寫了半天(當(dāng)然也可以用ASCII碼來實(shí)現(xiàn),會(huì)更方便點(diǎn))
接下來介紹一個(gè)方法,只需 一行超短代碼 即可實(shí)現(xiàn) " 隨機(jī)生成字符串 " 的功能
- const str = Math.random().toString(36).substr(2, 10);
- console.log(str); // 'w5jetivt7e'
我們同樣獲得了一個(gè)10位數(shù)的隨機(jī)字符串,這太酷了:sunglasses:,跟我寫的那個(gè)比起來,簡(jiǎn)直不要太爽
先是 Math.random() 生成 [0, 1) 的數(shù),也就是 0.123312 、 0.982931 之類的,然后調(diào)用 number 的 toString方法將其轉(zhuǎn)換成36進(jìn)制的,按照MDN的說法,36進(jìn)制的轉(zhuǎn)換應(yīng)該是包含了字母 a~z 和 數(shù)字 0~9 的,因?yàn)檫@樣生成的是 0.89kjna21sa 類似這樣的,所以要截取一下小數(shù)部分,即從索引 2 開始截取10個(gè)字符就是我們想要的隨機(jī)字符串了
很多開源庫都使用此方式為DOM元素創(chuàng)建隨機(jī)ID。
六、最快獲取dom的方法
HTML 中帶有 id 屬性的元素,都會(huì)被全局的 ID 同名變量所引用
原本獲取 dom 是這樣的
- const el = document.getElementById('zero2one')
- console.log(el) //
現(xiàn)在可以這樣
- console.log(zero2one) //
是不是很方便 ^-^
文章題目:JavaScript奇怪又實(shí)用的姿勢(shì)又增加了六個(gè)
網(wǎng)站地址:http://m.fisionsoft.com.cn/article/dhdcchh.html


咨詢
建站咨詢
