jsDoc 佈道
太長了;
使用遺留程式碼庫 - 我們中的許多人無法一次又一次地躲避 - 讓我嘗試使用 jsDoc 而不是 Typescript。我必須揭露令人驚訝的真相!
首先讓我們清理一下: jsDoc 或TS 只是意味著在開發人員時間下(包括稍後審查、重用、在任何環境中查看該程式碼:git 網頁、隨機編輯器、chrome 、firefox、. ..:devtool、vim、cat ... );
第一樂章
只需開啟編輯器,寫下適當的註解即可測試 jsDoc 是否正常運作。
/** @typedef { 'JS' | 'JQuery' | 'TS' | 'jsDoc' } Phase; */ /** * On typing ' editor will popup string list. * * @type {Phase} */ const badCase = 'React'; // !!!! lint alert !!!! /** @type {Phase} */ const goodCase = 'JQuery';
jsDoc 與 Typescript
根據我的經驗,jsDoc 能夠取代 TS 程式碼,此外兩者之間存在蟲洞。
最大的 jsDoc 功能imho:這是使用標準的 JS 註解系統,所以不要破壞任何 JS 程式碼,此程式碼將在 JS 能夠運行的任何地方運行。
feature | jsDoc | Typescript |
---|---|---|
compiling freedom | Do not need compiling the code. | mandatory to compile |
dependency freedom | Can working without any dependency | at least TS is your dependency |
namespace freedom | Don't interfere Types and other imports names. You can use same name of component and component Type in React for example. | Typesript names are identical including as any other referenct |
rework freedom | Don't need to change existing code, just insert a comment. | at least some part in your code need to be turn to TS |
legacy freedom | Can be use even transform to Typescript the project is not option. Many legacy projects affected this situation. | need to push management to allow that modification |
Maximalism freedom | Don't need to use every where. Even you can use on a new commits, and after easy to search which is the new or reworked parts. | also enough to turn build system, and just part of code to TS |
future freedom | Later can easy trasnlate to TS. Under the hood this is use same technic, just different way. | need to work if decide to use JS instead TS |
mindset freedom | Because this is a comment your know well this is don't made any type check at runtime for you as TS also. | TS is noising your code |
jsDoc 編輯經驗
我可以在任何編輯器中編寫jsDoc,但很少有人理解它。
節點模組體驗
我還建立了一個 npm 模組:jsdoc-duck:一個 jsDoc 編碼模組。這強調如果沒有 TypeScript,就不可能建立 jsDoc npm 模組。也許如果我花更多的時間來弄清楚 vite 建置參數,那麼就可以找到正確的解決方案。但好消息是,如果不在npm 中使用該模組,而是藉用我們的程式碼:只需將index.js 複製到某個位置- 那麼我們就可以將新的依賴項插入到我們的程式中,並且不會發生任何事情如果模組所有者將模組轉為其他東西。
jsDoc 之間的蟲洞打字稿
好消息是 Typescript 和 jsDoc 互相相容,只是 jsDoc 使用了一些不同的語法。但您可以在打字稿中使用 jsDoc 模組類型,也可以在 javascript / js jsDoc 程式中使用打字稿模組類型。所以最終決定權在你手中。
沿著黃磚路走
VS 程式碼範例展示瞭如何很好地看到 jsDoc 程式碼中的類型,在我看來,這給您的程式碼帶來的噪音令人驚訝地減少了。
獎金
:: VS 程式碼片段
/** @typedef { 'JS' | 'JQuery' | 'TS' | 'jsDoc' } Phase; */ /** * On typing ' editor will popup string list. * * @type {Phase} */ const badCase = 'React'; // !!!! lint alert !!!! /** @type {Phase} */ const goodCase = 'JQuery';
:: jsdoc-duck 程式碼
(在此視圖中語法突出顯示無助於理解類型)。但是這個簡短的程式是一個很好的例子,jsDoc 也可以使用 TS 的進階功能。
"@type": { "prefix": ["ty"], "body": ["/** @type {<pre class="brush:php;toolbar:false">import { useMemo, useReducer } from "react"; /** * @template T - Payload Type * @typedef {T extends { type: infer U, payload?: infer P } ? { type: U, payload?: P } : never} ActionType */ /** @template AM - Actions Map @typedef {{ [K in AM['type']]: K }} Labels */ /** @template AM - Actions Map @typedef {{ [T in AM["type"]]: Extract<AM, { type: T }> extends { payload: infer P } ? (payload: P) => void : () => void }} Quack */ /** * @template ST - State * @template AM - Actions Map * @typedef {(state: ST, action: AM) => ST} Reducer */ /** * Factory function to create a typed action map. * @template AM - Actions Map * @param {Labels<AM>} labelsObject - The keys representing action labels. * @param {function} dispatch - The dispatch function for actions. * @return {Quack<AM>} The resulting typed action map. */ export const quackFactory = (labelsObject, dispatch) => Object .keys(labelsObject) .reduce( /** * @arg {Quack<AM>} acc * @arg {keyof Labels<AM>} type * @return {Quack<AM>} */ (acc, type) => ({ ...acc, [type]: (payload) => {dispatch({ type, payload });} }), {}); /** * A factory hook to create a state and a typed dispatch functions\ * @exports useDuck * @template AM - Actions Map * @template ST - State Typer * @param {(st: ST, action: AM) => ST} reducer - The reducer function to manage the state. * @param {ST} initialState - The initial state value. * @return {[ST, Quack<AM>]} The current state and a map of action dispatch functions. */ export const useDuck = (reducer, initialState, labels) => { const [state, dispatch] = useReducer(reducer, initialState); const quack = useMemo( () => quackFactory(labels, dispatch), [dispatch, labels] ); return ([state, quack]); };
快樂編碼,挖掘而不是添加依賴
以上是jsDoc 佈道的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。

理解JavaScript引擎內部工作原理對開發者重要,因為它能幫助編寫更高效的代碼並理解性能瓶頸和優化策略。 1)引擎的工作流程包括解析、編譯和執行三個階段;2)執行過程中,引擎會進行動態優化,如內聯緩存和隱藏類;3)最佳實踐包括避免全局變量、優化循環、使用const和let,以及避免過度使用閉包。

Python和JavaScript在開發環境上的選擇都很重要。 1)Python的開發環境包括PyCharm、JupyterNotebook和Anaconda,適合數據科學和快速原型開發。 2)JavaScript的開發環境包括Node.js、VSCode和Webpack,適用於前端和後端開發。根據項目需求選擇合適的工具可以提高開發效率和項目成功率。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。1)C 用于解析JavaScript源码并生成抽象语法树。2)C 负责生成和执行字节码。3)C 实现JIT编译器,在运行时优化和编译热点代码,显著提高JavaScript的执行效率。

Python更適合數據科學和自動化,JavaScript更適合前端和全棧開發。 1.Python在數據科學和機器學習中表現出色,使用NumPy、Pandas等庫進行數據處理和建模。 2.Python在自動化和腳本編寫方面簡潔高效。 3.JavaScript在前端開發中不可或缺,用於構建動態網頁和單頁面應用。 4.JavaScript通過Node.js在後端開發中發揮作用,支持全棧開發。

JavaScript在網站、移動應用、桌面應用和服務器端編程中均有廣泛應用。 1)在網站開發中,JavaScript與HTML、CSS一起操作DOM,實現動態效果,並支持如jQuery、React等框架。 2)通過ReactNative和Ionic,JavaScript用於開發跨平台移動應用。 3)Electron框架使JavaScript能構建桌面應用。 4)Node.js讓JavaScript在服務器端運行,支持高並發請求。
