简体   繁体   English

在进行JavaScript大量工作时,将所有样式保留在CSS文件中有什么好处?

[英]What are the benefits of keeping all stylings in CSS files when doing Javascript intensive work?

There seems to be the belief out there that ALL stylings need to be kept in the CSS files. 似乎有人认为所有样式都需要保留在CSS文件中。 If I am doing purely JavaScript work, this does not make sense to me. 如果我只从事JavaScript工作,那对我来说就没有意义。 If I create a JavaScript object called : "StatisticSummaryPanel", surely it is better to keep the CSS stylings inside the JavaScript object. 如果我创建一个JavaScript对象:“ StatisticSummaryPanel”,则最好将CSS样式保留在JavaScript对象中。 That way I only have to maintain one file instead of two files. 这样,我只需要维护一个文件,而不是两个文件。

It's generally considered better practice to have your CSS, JavaScript and HTML in separate files as it makes it easier to locate, understand and debug for yourself and for potential future developers who come along and work on your files. 通常,将CSS,JavaScript和HTML放在单独的文件中被认为是更好的做法,因为它可以更轻松地为自己以及随身携带文件的潜在潜在开发人员查找,理解和调试。

In your JavaScript file it's just a cleaner approach to simply refer to a class or an ID that has it's properties stored in the CSS file. 在您的JavaScript文件中,只是引用其属性存储在CSS文件中的类或ID只是一种更清洁的方法。

If you reuse a class for example in multiple JavScript objects and you need to change the properties of that class then you only have to update the CSS file. 例如,如果您在多个JavScript对象中重用一个类,并且需要更改该类的属性,则只需更新CSS文件。

It helps if you need to reuse the same style in a different page/javascript file. 如果您需要在不同的页面/ javascript文件中重复使用相同的样式,它会有所帮助。 This way if your style changes, you need not change in all the files you use it. 这样,如果您的样式发生更改,则无需更改使用它的所有文件。

Also, it can be cached and make your page little faster. 此外,它可以被缓存,并使您的页面速度更快。

And coming to maintain more files, its a call based on size of your files. 为了维护更多文件,它会根据文件大小进行调用。 If it's too huge, a single file will be costly to maintain. 如果太大,则单个文件的维护成本很高。

There are several problems: 有几个问题:

1. Code legibility 1.代码易读性

Writing CSS or HTML in Javascript is verbose and difficult to read and edit compared to when written directly in its own syntax. 与直接以自己的语法编写相比,用Javascript编写CSS或HTML冗长且难以阅读和编辑。

2. Confusing behaviour and logic 2.令人困惑的行为和逻辑

A bare minimum of styling is necessary to achieve basic functionality, but appearance and logic shouldn't be confused. 要实现基本功能,最少需要样式设置,但不要混淆外观和逻辑。 Figuring out how and where to integrate code about appearancee into code about behaviour is a non-sensical task. 弄清楚如何以及在何处将有关外观的代码集成到有关行为的代码中是不明智的任务。 Your code will be much clearer without esoteric style requirements baked into your behavioural scripting. 无需将深奥的样式要求纳入行为脚本,您的代码将更加清晰。

3. Retrieval 3.检索

People don't expect to find CSS in Javascript. 人们不希望在Javascript中找到CSS。 When someone wants to try and change the appearance of the StatisticSummaryPanel , they're going to look for it in the CSS. 当有人想改变StatisticSummaryPanel的外观时,他们会在CSS中寻找它。 The last place they'll expect to find its stylistic definition is in the JS. 他们期望找到其样式定义的最后一个地方是JS。

Separation of concerns: in this case, the separation of presentation (how various effects appear and render) and behavior (the causes of said effects). 关注点分离:在这种情况下,表示(行为产生和呈现的各种方式)与行为(行为产生的原因)的分离。 In our experience, separating the two allows us to hire great graphic designers for the former, and great programmers for the latter. 根据我们的经验,将两者分开可以使我们为前者雇用优秀的图形设计师,而为后者雇用优秀的程序员。

For a small project, it probably doesn't matter, as you're the only one responsible for the code. 对于一个小型项目,这可能并不重要,因为您是唯一负责代码的人。 But if you plan to expand to a team, it's worth thinking about how different members may specialize, and how best to coordinate changes amongst your teammates. 但是,如果您打算扩展到团队,则值得考虑不同成员的专业程度,以及如何最好地协调团队成员之间的变化。

暂无
暂无

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

相关问题 Javascript / CSS:规范页面上的所有元素样式? - Javascript/CSS: normalize all element stylings on a page? 在将所有 Javascript 文件发送给客户端之前将其连接成一个有什么好处? - What are the benefits of concatenating all Javascript files into one before sending it to client? 使用Angular样式加载CSS - Loading CSS with Angular Stylings 将Javascript存储在外部文件中有什么好处? <head> ? - What benefits are there to storing Javascript in external files vs in the <head>? 我的JavaScript无法加载外部CSS文件。 我究竟做错了什么? - my javascript for loading external css files isn't working. what am i doing wrong? 在 JavaScript 中初始化变量有什么好处? - What are the benefits of initializing variables in JavaScript? JavaScript 中`in` 运算符的好处/优势是什么? - What are the benefits / advantages of `in` operator in JavaScript? Purge-css 正在删除所有 css 样式,而不仅仅是未使用的样式 - Purge-css is removing all css stylings instead of just the unused ones 我的 React 项目中可能缺少哪些 CSS 样式没有应用于我的组件? - What might be missing in my React project that the CSS stylings aren't getting applied to my components? 在使用HTTP / 2时,缩小和连接JS / CSS文件以及使用精灵图像仍能提供性能优势吗? - Does minifying and concatenating JS/CSS files, and using sprites for images still provide performance benefits when using HTTP/2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM