编写 Javascript / jQuery 的最佳方式

发布于 2024-12-31 21:36:46 字数 361 浏览 0 评论 0原文

我对网络开发并不陌生,但我只是想知道你们是否能给我一些改进的建议:

当我编写 js 或 jQuery 时,我总是在 中编写这样的所有代码> 在我的页面上:

<script>
$(document).ready(function() {

   //All my functions and animations goes here…

});
</script>

现在,当我环顾网络时,我发现大多数人都将其功能和动画放在单独的“.js”文件中。他们也经常创建课程。

这是如何运作的?创建类并将函数放在单独的文件中有何优点?

谢谢,VG

I'm not new to web dev but I was just wondering if you guys have some advice for me to improve:

Always when I write js or jQuery I write all the code like this in the <head> on my page:

<script>
$(document).ready(function() {

   //All my functions and animations goes here…

});
</script>

Now, when I look around the web I see that most people have their functions and animations in a separate ".js"-file. And they often create classes aswell.

How does that work? What is the advantage of creating classes and have your functions in a separate file?

Thanks, VG

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

逆蝶 2025-01-07 21:36:46

将 javascript 放入单独文件的优点:

  1. 在多个页面上重用 - 您可以更轻松地在多个页面上重用相同的 javascript 函数,而无需拥有多个单独的代码副本。
  2. 更高效的缓存 - 浏览器可以更有效地缓存您的 JavaScript,从而加快页面加载时间。使用外部 JS 文件时,可以在首次使用时加载它,然后从浏览器缓存中检索该文件,以供使用同一 JS 文件的所有后续页面加载。
  3. 代码与 HTML 分离 - 您可以更好地分离页面上不同类型的内容。视觉设计人员可以处理 HTML,而软件开发人员可以处理 javascript,但他们并不是在完全相同的文件中工作。

注意:出于性能原因,您不希望拥有大量小的、独立的外部 JS 文件,因为加载大量小的 JS 文件比加载一个较大的 JS 文件需要更长的时间。

将代码构建为类/对象可以具有以下优点:

  1. 组织 - 它迫使开发人员将其代码组织成功能块,其中对象具有给定的一组方法和属性。对于除琐碎项目以外的任何项目,这是一种比一大堆 JavaScript 函数更简洁的组织方式。
  2. 封装 - 面向对象的设计使您可以将数据放入对象中,这样就可以更清楚地知道谁拥有数据、谁修改数据、数据的生命周期是什么以及存在哪些方法来修改数据。此外,一些数据可以封装在对象内部,因此只能通过所需的方法进行修改(不能直接修改)。
  3. 测试 - 为组织成对象、方法和属性的代码编写测试套件会容易得多。
  4. 自记录 - 组织成对象、方法和属性的代码通常比一长串过程更具自记录性。
  5. 模块化 - 将面向对象的代码分解为可重用的模块通常比不以这种方式组织的代码更容易。
  6. 可扩展性 - 与直接的过程代码相比,良好的面向对象设计可以更容易地通过子类化和方法覆盖等方式进行扩展和增强。
  7. 可维护性 - 由于上述所有原因,良好的面向对象代码可以更容易维护。

仅供参考,面向对象的思维和设计的许多优点可以在不使用文字类的情况下找到(JavaScript 甚至没有真正的类)。这更多的是如何设计和实现代码的问题,而不是特定语言语法功能的问题。例如,您可以使用纯 C 语言进行面向对象的设计,它没有类或对象的特定语言概念。在 javascript 中,许多语言元素都是具有可扩展属性和方法的对象,因此在 javascript 中进行面向对象设计就更容易了(事实上,很难避免这样做),因为你周围已经有对象了(窗口对象、所有DOM 对象等...)。

Advantages of putting your javascript into a separate file:

  1. Reuse on multiple pages - You can more easily reuse the same javascript functions on multiple pages without having multiple separate copies of the code.
  2. More efficient caching - The browser can much more efficiently cache your javascript which can lead to faster page load times. When using an external JS file, it can be loaded upon first use and then retrieved from the browser cache for all subsequent page loads that use that same JS file.
  3. Separation of code from HTML - You get better separation of the different types of content on the page. The visual designer can work on the HTML while the software developer can work on the javascript and they are not working in the exactly same file.

Note: for performance reasons, you don't want to have a lot of small, separate external JS files as it takes longer to load lots of small JS files than it does one larger JS file.

Structuring code into classes/objects can have the following advantages:

  1. Organization - It forces the developer to organize their code into blocks of functionality where you have an object with a given set of methods and properties. For anything other than a trivial project, this is a much cleaner way or organizing things than just a big list of javascript functions.
  2. Encapsulation - Object oriented design causes you to put data into objects where it's much clearer who owns the data, who modifies the data, what it's lifetime is and what methods exist to modify the data. In addition, some data can be encapsulated inside the object so it can only be modified via the desired methods (not mucked with directly).
  3. Testing - it can be much easier to write test suites for code organized into objects, methods and properties.
  4. Self documenting - Code organized into objects, methods and properties is general much more self-documenting that a long list of procedures.
  5. Modularity - It is typically easier to break object-oriented code into reusable modules than code that is not organized this way.
  6. Extensibility - A good object oriented design can be much easier to extend and enhance through things like sub-classing and method overrides than straight procedural code.
  7. Maintainability - For all the above reasons, good object oriented code can be easier to maintain.

FYI, many of the advantages of object oriented thinking and design can be found without using literal classes (which javascript doesn't even really have). It's more a matter of how you design and implement your code than it is a particular language syntax feature. For example, you can have an object oriented design in plain C which has no specific language concept of a class or object. In javascript lots of language elements are objects with extensible properties and methods so it's even easier to do object oriented design in javascript (in fact, it's hard to avoid doing it) because there are already objects all around you (the window object, all the DOM objects, etc...).

分开我的手 2025-01-07 21:36:46

将 javascript 代码放在单独的文件中的优点是您不再混合标记(这就是 HTML)和脚本。您有明确的关注点分离。 CSS 也是如此。样式分为单独的文件。结果你的标记变得更小。 JavaScript 是静态的,客户端浏览器会缓存所有这些外部静态文件。因此他们不需要每次发出请求时都获取它。这可以提高应用程序的性能,因为通过线路发送的数据更少。仅从服务器检索逐页更改的标记。

至于将脚本组织成类和函数,这是为了更好的可读性和可重用性。当您正在处理包含大量 JavaScript 的大型项目时,最好将其拆分为单独的模块,以便团队中的不同开发人员可以更轻松地维护这些模块。

The advantage of putting your javascript code in separate files is that you no longer mix markup (which is what HTML is) and scripting. You have a clear separation of concerns. Same stands for CSS. Styling goes into separate files. As a result your markup becomes smaller. The javascript being static, client browsers are caching all those external static files. So they don't need to fetch it everytime a request is made. This improves the performance of your applications because there is less data to send over the wire. Only the markup that changes from page to page is retrieved from the server.

As far as organizing your scripts into classes and functions, that's for better readability and reusability. When you are working on large projects with lots of javascript it is better if you could split it into separate modules so that those modules could be easier to maintain by different developers on the team.

凉薄对峙 2025-01-07 21:36:46

将类和非类拆分成单独的文件可以使其他项目更易于维护和重用。它只是让事情井井有条。

但请注意...您不希望客户端必须下载大量文件!很多时候,服务器端会运行一些东西来将所有相关文件组装到一个下载中。 PHP 可以轻松地为您做到这一点。

Splitting up your classes and what not into separate files makes the more maintainable and more reuseable for other projects. It just keeps things organized.

Beware though... you don't want to have a ton of files the client has to go download! Often times, something will be running server-side to assemble all of the relevant files into one download. PHP can do this for you easily.

送舟行 2025-01-07 21:36:46

此外,实现 js 对象类有助于避免与变量和变量的命名空间冲突。函数名称...总而言之,在我看来这是一个很好的实践。

In addition, implementing js Object classes helps avoid namespace collision with variable & function names... all in all, in my opinion a good practice to get into.

青瓷清茶倾城歌 2025-01-07 21:36:46

有很多差异和更好的实践。但例如您希望 jquery 函数位于另一个文件中,您可以编写一次代码。此外,它还可以提高您网站的速度,只需加载一次内容。

quite a lot of difference and better practice. But for example you whant that jquery function to be in another file you could write the code once. Also it will increase the speed of your website with having only to load the content once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文