简体   繁体   English

在几个文件中拆分JavaScript代码:最佳实践

[英]Splitting JavaScript code in several files: best practices

Im working only 2 weeks with ExtJS. 我和ExtJS只工作了2周。 And all my code looks like one big file. 我的所有代码看起来都像一个大文件。 I have many included panels and other ExtJS objects in code. 我在代码中包含许多面板和其他ExtJS对象。 Looks like a big tree ) Can any give some simple advices for defining, storing code parts of JS files? 看起来像一棵大树)可以为定义,存储JS文件的代码部分提供一些简单的建议吗?

Things that fit into their own file fall into two broad categories: 适合自己文件的内容分为两大类:

Models 楷模

Any object constructor function. 任何对象构造函数。 Any set of functions that handle data. 处理数据的任何功能集。 Any business logic. 任何业务逻辑。 Validation logic. 验证逻辑。

Anything that handles logically manipulating data on a page without writing / reading from the DOM. 任何处理逻辑操作页面上的数据而不从DOM写入/读取的东西。

Views 查看

Anything that renders to the page. 任何渲染到页面的东西。 Template files. 模板文件。 anything that manipulates DOM objects. 任何操纵DOM对象的东西。

There are also miscellaneous things that fit into their own files 还有其他适合自己文件的东西

  • Server-Client communication, Websockets, ajax. 服务器 - 客户端通信,Websockets,ajax。
  • Micro frameworks 微框架
  • Utility belts 实用皮带
  • Routing helpers. 路由助手。

There are some things which are difficult to insolate into their owns files like the event based messaging that links your views to your models. 有些东西难以渗透到他们的拥有文件中,比如将您的视图链接到模型的基于事件的消息传递。

Generally you want to either use a packing tool to mix all your small files into one large file to send to the server or use a module loader like require . 通常,您希望使用打包工具将所有小文件混合到一个大文件中以发送到服务器或使用像require这样的模块加载器。

Anything that you think can be modularized can be placed into its own file. 您认为可以模块化的任何内容都可以放入自己的文件中。

If you find yourself repeating code you can make those classes, and those can go into their own file. 如果你发现自己重复代码,你可以创建这些类,那些可以进入他们自己的文件。 In addition you can split along panel lines regardless of whether they're repeated or not. 此外,无论是否重复,您都可以沿面板线分割。

So for an application teaching users how to turn their minimally trained grizzly bears into lifetime companions, you might end up with files like this: 因此,对于教授用户如何将经过极度训练的灰熊变成终身伴侣的应用程序,您最终可能会得到如下文件:

  • BearsAreNotDeadly.js BearsAreNotDeadly.js
  • Search.js Search.js
  • Tutorials.js Tutorials.js
  • GruesomePictures.js GruesomePictures.js
  • ComplaintForm.js ComplaintForm.js

... with each of those being a part of the application. ......每个人都是申请的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM