![](/img/trans.png)
[英]What are the best practices for writing and organizing javascript plugins?
[英]Import into an object? Best practices for organizing a module library
您好,我想創建一個帶有一組輔助函數的模塊庫。 不確定將代碼放在哪里以保持一切井井有條,首先想到的是創建它:
lib-name
- main.js
- some-module
- some-part-of-the-module.js
- some-other-part-of-the-module.js
但是,在訪問庫中的對象時,我希望能夠使用點范圍,比如
lib-name.some-module.some-thing("This is a function argument!");
既要避免違反 ODR 又要避免有 10-50 行的導入。
我最好如何設置它? 希望有一個完全普通的解決方案,盡管如果有任何通用庫可以解決這類問題,我會洗耳恭聽。 我完全沒有前端框架並使用 flask 后端編寫我的項目(我認為后者不相關?)。 理想情況下,至少要避免使用構建系統。
已經在https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules看了一點,但似乎import
不支持導入現有對象 - 很難實現我的。 訪問者的夢想沒有太多的麻煩。
這只是我的想法,可能有更好的方法來做到這一點。 從我的評論繼續,這里我會做什么:
libname/
some-module/
some-part-of-the-module.js
some-other-part-of-the-module.js
index.js
index.js
是橋接文件。 里面會是這樣的:
import { someFunction } from './some-module/some-part-of-the-module.js';
import { someOtherFunction } from './some-module/some-other-part-of-the-module.js';
const libname = {
someFunction,
someOtherFunction
}
export default libname;
當你想使用時,你會做類似的事情:
import libname from "libname";
libname.someFunction();
libname.someOtherFunction();
注意:橋接文件名為index.js
,因為當您從中導入內容時,不需要指定文件名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.