Commonly accepted best practices around code organization in JavaScript [closed]

問題:

As JavaScript frameworks like jQuery make client side web applications richer and more functional, I've started to notice one problem... 隨着像jQuery這樣的JavaScript框架使客戶端Web應用程序更豐富,更實用,我開始注意到一個問題......

How in the world do you keep this organized? 你是如何保持這種組織的?

  • Put all your handlers in one spot and write functions for all the events? 將所有處理程序放在一個位置併爲所有事件編寫函數?
  • Create function/classes to wrap all your functionality? 創建函數/類來包裝所有功能?
  • Write like crazy and just hope it works out for the best? 寫得像瘋了似的,只希望它能做到最好?
  • Give up and get a new career? 放棄並獲得新的職業?

I mention jQuery, but it's really any JavaScript code in general. 我提到了jQuery,但它實際上是任何JavaScript代碼。 I'm finding that as lines upon lines begin to pile up, it gets harder to manage the script files or find what you are looking for. 我發現隨着線條開始堆積起來,管理腳本文件或找到你要找的東西變得更加困難。 Quite possibly the biggest propblems I've found is there are so many ways to do the same thing, it's hard to know which one is the current commonly accepted best practice. 我發現最大的問題可能是有很多方法可以做同樣的事情,很難知道哪一個是目前普遍接受的最佳實踐。

Are there any general recommendations on the best way to keep your .js files as nice and neat as the rest of your application? 是否有關於保持.js文件與應用程序其餘部分一樣美觀和整潔的最佳方法的一般建議? Or is this just a matter of IDE? 或者這僅僅是IDE的問題? Is there a better option out there? 那裏有更好的選擇嗎?


EDIT 編輯

This question was intended to be more about code organization and not file organization. 這個問題旨在更多地關注代碼組織而不是文件組織。 There has been some really good examples of merging files or splitting content around. 有一些非常好的合併文件或分割內容的例子。

My question is: what is the current commonly accepted best practice way to organize your actual code? 我的問題是:目前普遍接受的組織實際代碼的最佳實踐方法是什麼? What is your way, or even a recommended way to interact with page elements and create reuseable code that doesn't conflict with each other? 您的方式是什麼,甚至是推薦的方式與頁面元素交互並創建不會相互衝突的可重用代碼?

Some people have listed namespaces which is a good idea. 有些人列出了名稱空間 ,這是一個好主意。 What are some other ways, more specifically dealing with elements on the page and keeping the code organized and neat? 還有什麼其他方法,更具體地說是處理頁面上的元素並保持代碼整潔有序?


解決方案:

參考一: https://en.stackoom.com/question/12JF
參考二: https://stackoom.com/question/12JF
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章