How do I properly organize JavaScript files for web development?

I am fairly new to web development and have been trying to figure out the best way to organize my JavaScript files for the front-end.

The first approach I started with was “one JavaScript file per page”, avoiding in-line scripts. Each file would usually attach event listeners and handle any initialization that needed to occur on the page. This was fine until the files started getting huge, and I found myself cordoning off sections for specific page “components” (e.g. a specific table, or a modal).

// SomePage.js

...

//--- User Table Section ---
const user_table = document.getElementById("user_table");
user_table.attachEventListener( ... )

...

//--- Delete Confirmation Section ---
const delete_confirmation = document.getElementById("delete_confirmation");
delete_confirmation.attachEventListener( ... )

...

This prompted me to throw together a “one JavaScript class per component” approach. I define a block of HTML (usually a PartialView) as a component by giving it a data attribute that specifies the JavaScript class that would operate exclusively on it. Each website page loads a single JavaScript file (the “entry point”) which searches for this data attribute and uses a mapping of component names to classes to instantiate the appropriate class. The end result is a collection of smaller and more focused script files.

// UserTable.js
export default class UserTable {
    constructor() {
        const user_table = document.getElementById("user_table");
        user_table.attachEventListener( ... )
    }
    // Helper functions go here...
}

// DeleteConfirmation.js
export default class DeleteConfirmation {
    constructor() {
        const delete_confirmation = document.getElementById("delete_confirmation");
        delete_confirmation.attachEventListener( ... )
    }
    // Helper functions go here...
}

// Entrypoint.js
// ...import classes here...
const componentMap = {
    "UserTable": UserTable,
    "DeleteConfirmation": DeleteConfirmation
}
// ...search the page for 'data-component="<component name>"' and call 'new componentMap[<name>]();'

(I don’t use a <script> tag in each component’s HTML because I believe it would be detrimental to loading times (components are usually PartialViews, so the browser would have to stop processing the HTML and load the script instead), or not work well with bundling since the “defer” attribute requires defining a specific script– and bundling everything into a single compressed file seems to be standard practice.)

However, this implementation has some problems.

The first is constructing the mapping between the data attribute values and the classes. I can’t see any way other than having to manually add a new entry each time I develop a new component. For example, if I create a “UserTable.js” that contains all the JavaScript that operates on the HTML enclosed by a parent with the data-component="UserTable" attribute, I have to go to the entry point file and add "UserTable": UserTable where the value is a reference to the appropriate class. To be honest, this is a relatively small issue, but it makes me wonder if there is a better way of handling things.

The second is that I find myself spending a lot more time trying to figure out the rules I need to follow for this approach to continue working in the future. Things like “should a page allow multiple of the same component?”, “should components be allowed to contain other components?”, “should I add the ability for components to search for other components on the page, or should every component be fully encapsulated?”, etc. These all have their own advantages and disadvantages, which makes it hard to decide when I’m not even sure if my current approach is reasonable or not.

There are some other things I’m not totally certain about, like whether I should be exporting classes or functions (at times, I feel like I’m only using the class for the constructor), or how much I should be caring about caching and minimizing server requests– but I think my confusion can be cleared up with just a good example of how I’m supposed to be organizing front-end JavaScript files. I’m pretty sure there’s a very simple and obvious solution that I’m overlooking, but I can’t seem to find it on the Internet.

New contributor

RotundChinchilla is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật