JavaScript中代码组织的普遍接受的最佳实践[关闭]

江枫思渺然 提交于 2020-08-05 03:52:19

问题:

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://stackoom.com/question/12JF/JavaScript中代码组织的普遍接受的最佳实践-关闭
参考二: https://oldbug.net/q/12JF/Commonly-accepted-best-practices-around-code-organization-in-JavaScript-closed
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!