编写 Javascript / jQuery 的最佳方式
我对网络开发并不陌生,但我只是想知道你们是否能给我一些改进的建议:
当我编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将 javascript 放入单独文件的优点:
注意:出于性能原因,您不希望拥有大量小的、独立的外部 JS 文件,因为加载大量小的 JS 文件比加载一个较大的 JS 文件需要更长的时间。
将代码构建为类/对象可以具有以下优点:
仅供参考,面向对象的思维和设计的许多优点可以在不使用文字类的情况下找到(JavaScript 甚至没有真正的类)。这更多的是如何设计和实现代码的问题,而不是特定语言语法功能的问题。例如,您可以使用纯 C 语言进行面向对象的设计,它没有类或对象的特定语言概念。在 javascript 中,许多语言元素都是具有可扩展属性和方法的对象,因此在 javascript 中进行面向对象设计就更容易了(事实上,很难避免这样做),因为你周围已经有对象了(窗口对象、所有DOM 对象等...)。
Advantages of putting your javascript into a separate 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:
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...).
将 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.
将类和非类拆分成单独的文件可以使其他项目更易于维护和重用。它只是让事情井井有条。
但请注意...您不希望客户端必须下载大量文件!很多时候,服务器端会运行一些东西来将所有相关文件组装到一个下载中。 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.
此外,实现 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.
有很多差异和更好的实践。但例如您希望 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.