简体   繁体   English

jQuery编码约定

[英]jQuery Coding Conventions

I've been looking for some jQuery coding conventions, but really found a comprehensive list. 我一直在寻找一些jQuery编码约定,但确实找到了完整的列表。

For example, how should I name the id of my html tags, I like the first one because it confoms to my css convention, but would it give me inconvenience in some senarios? 例如,我应该如何命名html标记的ID,我喜欢第一个,因为它混淆了我的CSS约定,但是会给我带来一些不便吗?

<div id="comment-list"></div>
<div id="commentList"></div>
<div id="CommentList"></div>

And, is single or double quote more preferrable $('#obj') vs $("#obj")? 并且,单引号或双引号是$('#obj')还是$(“#obj”)首选?

Also, say I use one global file, should I surroud all my code with the following to avoid $ sign conflict if I were to use other js libs? 另外,假设我使用一个全局文件,如果我要使用其他js库,是否应该用以下代码替换所有代码,以避免$符号冲突?

(function ($) {
   $(document).ready(function () {
     ...
   });
})(jQuery);

Should I use more js files or put everything in one global file? 我应该使用更多的js文件还是将所有内容都放在一个全局文件中?

etc. What are some of your conventions by following that made your coding in jQuery on large project easier? 等。遵循哪些使您在大型项目上使用jQuery编码更容易的约定?

I always use your preferred css convention - and have used jQuery on those elements without any issues. 我总是使用您首选的CSS约定-并在这些元素上使用jQuery而没有任何问题。 In terms of single/double quotes - I only use single quotes when the value contains HTML markup. 关于单引号/双引号-仅当值包含HTML标记时才使用单引号。 ie

$("#el").html('<div class="wrap">Hey!</div>');

I notice you updated your question after I answered it. 我注意到您在回答问题后更新了问题。

For multi-library conflicts you can use the libraries in compatibility mode. 对于多库冲突,您可以在兼容模式下使用这些库。 For example: 例如:

var $j = jQuery.noConflict();
$j(document).ready(function () {
   $j("body").css('background', 'red');
});

As for using single/multiple files it really comes down to tidyness vs performance. 至于使用单个/多个文件,实际上归结为整洁与性能。

Each new .js file will require an extra request to the server, thus slowing down your site. 每个新的.js文件将需要对服务器的额外请求,从而降低您的网站速度。 But if you were to write 5000 lines of code that do many many different things, you could certainly consider organizing them in multiple files. 但是,如果要编写5000行代码来完成许多不同的事情,则可以考虑将它们组织在多个文件中。

In these cases though, what I prefer to do is keep my scripts separate in my development environment, then combine them into one global.js file which I then minify for production. 但是,在这些情况下,我更喜欢在开发环境中将脚本分开,然后将它们组合到一个global.js文件中,然后将其最小化以进行生产。

HTH, Marko HTH,Marko

In HTML, XML, CSS, and URLs, multi-word names are typically "dasherized" (where the words are separated by dashes). 在HTML,XML,CSS和URL中,多词名称通常是“破译”的(单词之间用破折号分隔)。 In these cases, dasherizing your own multi-word names is most in keeping with the context. 在这些情况下,对自己的多词名称进行反串处理最符合上下文。

<div id="comment-list">...</div>

In JavaScript, single- and double-quotes are identical. 在JavaScript中,单引号和双引号是相同的。 Choose the most natural way of doing it, but be consistent about it. 选择最自然的方式,但要保持一致。

You should write your code cleanly - in multiple files. 您应该清晰地编写代码-包含多个文件。 In development and test, use the multiple files. 在开发和测试中,请使用多个文件。 For production (and staging sites), you may concatenate and minify your JavaScript and CSS files. 对于生产(和暂存站点),您可以串联并缩小JavaScript和CSS文件。 Best practice is to make use of a helper function to write out the HTML linking to these scripts and stylesheets, but which is smart enough to use the multiple files in development and test and the processed files in production. 最佳实践是利用辅助函数来编写指向这些脚本和样式表的HTML链接,但这种方法足够聪明,可以在开发和测试中使用多个文件,在生产中使用已处理的文件。

<%= javascript "jquery", "jquery-ui", "application", "users-page" %>

我通常在类名中使用-s,在ID中使用_s。

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

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