自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

這些 JS 的新語法有點(diǎn)東西啊

開發(fā) 前端
這是個(gè)挺不錯(cuò)的新語法。其他有些語言是可以用 arr[-1] 來獲取數(shù)組末尾的元素,但是對(duì)于 JS 來說這是實(shí)現(xiàn)不了的事情。因?yàn)?[key] 對(duì)于對(duì)象來說就是在獲取 key 對(duì)應(yīng)的值。

 [[420374]]

TC39 的提案筆者一直有關(guān)注,攢了一些有趣的今天來聊聊。

PS:提案總共五個(gè)階段,只有到階段 4 才會(huì)被納入到發(fā)布規(guī)范中,其它的只是有幾率會(huì)被納入。

.at()

這是個(gè)挺不錯(cuò)的新語法。其他有些語言是可以用 arr[-1] 來獲取數(shù)組末尾的元素,但是對(duì)于 JS 來說這是實(shí)現(xiàn)不了的事情。因?yàn)?[key] 對(duì)于對(duì)象來說就是在獲取 key 對(duì)應(yīng)的值。數(shù)組也是對(duì)象,對(duì)于數(shù)組使用 arr[-1] 就是在獲取 key 為 -1 的值。

由于以上原因,我們想獲取末尾元素就得這樣寫 arr[arr.length - 1],以后有了 at 這個(gè)方法,我們就可以通過 arr.at(-1) 來拿末尾的元素了,另外同樣適用類數(shù)組、字符串。 

  1. // Polyfill  
  2. function at(n) {  
  3.     // ToInteger() abstract op  
  4.     n = Math.trunc(n) || 0;  
  5.     // Allow negative indexing from the end  
  6.     if(n < 0) n += this.length;  
  7.     // OOB access is guaranteed to return undefined  
  8.     if(n < 0 || n >= this.length) return undefined;  
  9.     // Otherwise, this is just normal property access  
  10.     return this[n];  

頂層 await

await 都得用 async 函數(shù)包裹大家肯定都知道,這個(gè)限制導(dǎo)致我們不能在全局作用域下直接使用 await,必須得包裝一下。

有了這個(gè)提案以后,大家就可以直接在頂層寫 await 了,算是一個(gè)便利性的提案。

目前該提案已經(jīng)進(jìn)入階段 4,板上釘釘會(huì)發(fā)布。另外其實(shí) Chrome 近期的更新已經(jīng)支持了該功能。

image-20210620162451146

Error Cause

這個(gè)語法主要幫助我們便捷地傳遞 Error。一旦可能出錯(cuò)的地方一多,我們實(shí)際就不清楚錯(cuò)誤到底是哪里產(chǎn)生的。如果希望外部清楚的知道上下文信息的話,我們需要封裝以下 error。 

  1. async function getSolution() {  
  2.   const rawResource = await fetch('//domain/resource-a')  
  3.     .catch(err => {  
  4.       // How to wrap the error properly?  
  5.       // 1. throw new Error('Download raw resource failed: ' + err.message); 
  6.       // 2. const wrapErr = new Error('Download raw resource failed');  
  7.       //    wrapErr.cause = err
  8.       //    throw wrapErr;  
  9.       // 3. class CustomError extends Error {  
  10.       //      constructor(msg, cause) {  
  11.       //        super(msg);  
  12.       //        this.cause = cause;  
  13.       //      } 
  14.       //    }  
  15.       //    throw new CustomError('Download raw resource failed', err);  
  16.     }) 
  17.    const jobResult = doComputationalHeavyJob(rawResource);  
  18.   await fetch('//domain/upload', { method: 'POST', body: jobResult });  
  19. await doJob(); // => TypeError: Failed to fetch 

那么有了這個(gè)語法以后,我們可以這樣來簡(jiǎn)化代碼: 

  1. async function doJob() {  
  2.   const rawResource = await fetch('//domain/resource-a')  
  3.     .catch(err => {  
  4.       throw new Error('Download raw resource failed', { cause: err });  
  5.     }); 
  6.   const jobResult = doComputationalHeavyJob(rawResource);  
  7.   await fetch('//domain/upload', { method: 'POST', body: jobResult })  
  8.     .catch(err => {  
  9.       throw new Error('Upload job result failed', { cause: err });  
  10.     });  
  11.  
  12. try {  
  13.   await doJob();  
  14. } catch (e) {  
  15.   console.log(e);  
  16.   console.log('Caused by', e.cause);  
  17.  
  18. // Error: Upload job result failed  
  19. // Caused by TypeError: Failed to fetch 

管道運(yùn)算符

這個(gè)語法的 Star 特別多,有 5k 多個(gè),側(cè)面也能說明是個(gè)受歡迎的語法,但是距離發(fā)布應(yīng)該還有好久,畢竟這個(gè)提案三四年前就有了,目前還只到階段 1。

這個(gè)語法其實(shí)在其他函數(shù)式編程語言上很常見,主要是為了函數(shù)調(diào)用方便: 

  1. let result = exclaim(capitalize(doubleSay("hello")));  
  2. result //=> "Hello, hello!"  
  3. let result = "hello"  
  4.   |> doubleSay  
  5.   |> capitalize  
  6.   |> exclaim;  
  7. result //=> "Hello, hello!" 

這只是對(duì)于單個(gè)參數(shù)的用法,其它的用法有興趣的讀者可以自行閱讀提案,其中涉及到了特別多的內(nèi)容,這大概也是導(dǎo)致推進(jìn)階段慢的原因吧。

新的數(shù)據(jù)結(jié)構(gòu):Records & Tuples

這個(gè)數(shù)據(jù)結(jié)構(gòu)筆者覺得發(fā)布以后會(huì)特別有用,總共新增了兩種數(shù)據(jù)結(jié)構(gòu),我們可以通過 # 來聲明:

1. #{ x: 1, y: 2 }2.#[1, 2, 3, 4]

這種數(shù)據(jù)結(jié)構(gòu)是不可變的,類似 React 中為了做性能優(yōu)化會(huì)引入的 immer 或者 immutable.js,其中的值只接受基本類型或者同是不可變的數(shù)據(jù)類型。 

  1. const proposal = #{  
  2.   id: 1234,  
  3.   title: "Record & Tuple proposal",  
  4.   contents: `...`,  
  5.   // tuples are primitive types so you can put them in records:  
  6.   keywords: #["ecma", "tc39", "proposal", "record", "tuple"],  
  7. };  
  8. // Accessing keys like you would with objects!  
  9. console.log(proposal.title); // Record & Tuple proposal  
  10. console.log(proposal.keywords[1]); // tc39  
  11. // Spread like objects!  
  12. const proposal2 = #{  
  13.   ...proposal,  
  14.   title: "Stage 2: Record & Tuple",  
  15. };  
  16. console.log(proposal2.title); // Stage 2: Record & Tuple  
  17. console.log(proposal2.keywords[1]); // tc39  
  18. // Object functions work on Records:  
  19. console.log(Object.keys(proposal)); // ["contents", "id", "keywords", "title"] 

最后

以上筆者列舉了一部分有意思的 TC39 提案,除了以上這些還有很多提案,各位讀者有興趣的話可以在 TC39 中尋找。 

 

責(zé)任編輯:龐桂玉 來源: 前端大全
相關(guān)推薦

2020-12-14 05:57:01

clipboard.Selection execCommand

2024-06-13 10:24:28

2024-06-14 08:08:02

2022-08-19 12:12:02

TypeScriptInfer 類型

2021-10-15 10:26:28

鴻蒙HarmonyOS應(yīng)用

2010-03-29 10:45:48

HTML 5

2024-06-19 08:45:13

2010-09-09 15:32:48

SQL插入數(shù)據(jù)

2009-07-08 18:07:58

jvm jre

2023-11-06 19:00:17

Python

2024-03-15 08:45:31

Vue 3setup語法

2021-12-28 08:46:00

Vue3refreactive

2021-04-06 21:30:56

代碼SPI重構(gòu)

2012-05-22 01:49:22

Highlight.jJavaWEB

2021-12-05 23:17:18

iOS蘋果系統(tǒng)

2017-09-26 10:00:15

前端JS語法

2019-03-26 09:20:12

蘋果 iOS系統(tǒng)

2021-01-03 09:44:34

解壓軟件解壓神器應(yīng)用

2024-05-13 08:04:26

Vue.jsWeb應(yīng)用程序

2023-01-30 09:01:34

DecoratorsJS語法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)