前端开发规范整理

来源:互联网 发布:python google语音api 编辑:程序博客网 时间:2024/06/06 01:50

驼峰式命名法介绍

  1. Pascal Case 大驼峰式命名法:首字母大写。eg:StudentInfo、UserInfo、ProductInfo Camel
  2. Case 小驼峰式命名法:首字母小写。eg:studentInfo、userInfo、productInfo

变量命名
命名方式 : 小驼峰式命名方法
命名规范 : 类型+对象描述的方式,如果没有明确的类型,就可以使前缀为名词

函数
命名方式 : 小驼峰方式 ( 构造函数使用大驼峰命名法 )
命名规则 : 前缀为动词

常量
命名方法 : 全部大写
命名规范 : 使用大写字母和下划线来组合命名,下划线用以分割单词。
推荐:

 var MAX_COUNT = 10; var URL = 'http://www.baidu.com';

文件资源命名

文件名不得含有空格
文件名建议只使用小写字母,不使用大写字母。( 为了醒目可以使用大写字母,比如README、LICENSE。 )
文件名包含多个单词时,单词之间建议使用半角的连词线 ( - ) 分隔。
引入资源使用相对路径,不要指定资源所带的具体协议 ( http:,https: ) ,除非这两者协议都不可用。

类的成员
公共属性和方法 : 同变量命名方式
私有属性和方法 : 前缀为下划线(_)后面跟公共属性和方法一样的命名方式
推荐(将name换成this是不是更熟悉了呢)

function Student(name) {    var _name = name; // 私有成员    // 公共方法    this.getName = function () {        return _name;    }    // 公共方式    this.setName = function (value) {        _name = value;    }}var st = new Student('tom');st.setName('jerry');console.log(st.getName()); // => jerry:输出_name私有变量的值

函数(方法)注释

函数(方法)注释也是多行注释的一种,但是包含了特殊的注释要求,参照 javadoc(百度百科)
语法:

/**
* 函数说明
* @关键字
*/
常用注释关键字

这里写图片描述

这里写图片描述

脚本加载

这里写图片描述

html
html 代码:

<!-- The page header should go into a header element --><header>  <!-- As this title belongs to the page structure it's a heading and h1 should be used -->  <h1>My page title</h1></header><!-- All navigation should go into a nav element --><nav class="top-navigation">  <!-- A listing of elements should always go to UL (OL for ordered listings) -->  <ul>    <li class="nav-item"><a href="#home">Home</a></li>    <li class="nav-item"><a href="#news">News</a></li>    <li class="nav-item"><a href="#about">About</a></li>  </ul></nav><!-- The main part of the page should go into a main element (also use role="main" for accessibility) --><main class="news-page" role="main">  <!-- A section of a page should go into a section element. Divide a page into sections with semantic elements. -->  <section class="page-section news">    <!-- A section header should go into a section element -->    <header>      <!-- As a page section belongs to the page structure heading elements should be used (in this case h2) -->      <h2 class="title">All news articles</h2>    </header>    <!-- If a section / module can be seen as an article (news article, blog entry, products teaser, any other     re-usable module / section that can occur multiple times on a page) a article element should be used -->    <article class="news-article">      <!-- An article can contain a header that contains the summary / introduction information of the article -->      <header>        <!-- As a article title does not belong to the overall page structure there should not be any heading tag! -->        <div class="article-title">Good article</div>        <!-- Small can optionally be used to reduce importance -->        <small class="intro">Introduction sub-title</small>      </header>      <!-- For the main content in a section or article there is no semantic element -->      <div class="content">        <p>This is a good example for HTML semantics</p>      </div>      <!-- For content that is represented as side note or less important information in a given context use aside -->      <aside class="article-side-notes">        <p>I think I'm more on the side and should not receive the main credits</p>      </aside>      <!-- Articles can also contain footers. If you have footnotes for an article place them into a footer element -->      <footer class="article-foot-notes">        <!-- The time element can be used to annotate a timestamp. Use the datetime attribute to specify ISO time         while the actual text in the time element can also be more human readable / relative -->        <p>This article was created by David <time datetime="2014-01-01 00:00" class="time">1 month ago</time></p>      </footer>    </article>    <!-- In a section, footnotes or similar information can also go into a footer element -->    <footer class="section-footer">      <p>Related sections: Events, Public holidays</p>    </footer>  </section></main><!-- Your page footer should go into a global footer element --><footer class="page-footer">  Copyright 2014</footer>

alt标签不为空 ,结构、表现、行为三者分离
这里写图片描述

**js规范
避免全局命名空间污染**

推荐的IIFE写法:(function(){  'use strict';  // Code goes here}());

文章来自:https://juejin.im/post/592d4a5b0ce463006b43b6da

http://www.imooc.com/article/5219

css统一命名
  注:本CSS命名规则只适合物table制作模式下
  1、 css文件统一放在css文件夹下;命名css.css
  2、 主样式定义:body、table、td、tr、a
  3、 链接样式定义:link_white(白色);link_black (黑色);link_blue (蓝色) 等等;
  说明:如有重复的后面加阿拉伯数字;如 link_red01 有下划线的如:linkred
  4、 文字样式定义:font_red (红色);font_red_14 (红色14号字);font_red_14b (红色14号加粗)
  5、 边框样式定义:border_red_tblr (红色四个边);border_red_blr(红色底左右三边);border_red_lr(红色左右两个边);border_red_b (红色底一个边)等等;
  6、 表单样式定义:text_100 (文本字段宽为100); textarea_200_red (文本区域宽为200有红色边框);select_100 (列表宽为100);button_150 (按钮宽150);
  说明:表单用宽度定义,在命名中最长写到:“text_100_red”
  7、 线的样式定义:line_X (横线);line_Y (竖线);line_X_red (红色横线);line_X_red2 (两个像素的红色横线);说明:在line中只定义虚线,实线在border中定义
  8、 其它样式定义:在这里主要定义一些个性化的样式;
  文件命名
  head.asp 头文件
  foot.asp 底文件
  index.asp 首页文件
  sort.html 分类嵌套文件
  article_channel.asp 文章_频道页
  article_list.asp 文章_列表页
  article_detail.asp 文章_显示页
  注明:如果有多个文章频道,则用article01, article02,article03等等
  exhibit_channel.asp 展会信息_频道页
  exhibit_list.asp 展会信息_列表页
  exhibit_detail.asp 展会信息_显示页
  product_channel.asp 产品中心_频道页
  product_list.asp 产品中心_列表页
  prodect_detail.asp 产品中心_显示页
  corporation_channel.asp 会员_频道页
  corporation_list.asp 会员_列表页
  corporation_detail.asp 会员_显示页
  information_channel.asp 商机信息_频道页
  information_list.asp 商机信息_列表页
  information_detail.asp 商机信息_显示页
  job_channel.asp 招聘_频道页
  job_list.asp 招聘_列表页
  job_detail.asp 招聘_显示页
  hr_channel.asp 求职_频道页
  hr_list.asp 求职_列表页
  hr_detail.asp 求职_显示页
  job_hr_channel.asp 人才中心_频道页
  job_hr_lisr.asp 人才中心_列表页
  job_hr_detail.asp 人才中心显示页
  copyright.asp 版权页
  图片命名
  1、导航命名:menu.gif 如:menubg .gif(导航的背景图)
  2、会员登录:login.gif 如:loginbg.gif (会员登陆的背景图)
  3、搜索命名:search.gif 如:search_bg.gif (搜索的背景图)
  4、小 图 标:ico_数字.gif 如:ico_001.gif
  5、线的命名:line_X_颜色.gif 如:line_X_red.gif(红色横向虚线)说明:line只命名虚线
  line_Y_red.gif(红色纵向虚线)
  6、广告命名:ad_数字.gif 如:ad001.gif
  7、其它栏目的图片:以栏目名的第一个字母.gif
  如:xwzx_bg.gif (新闻中心背景) cpzx_l.gif (产品中心的左边图)
  8、产品与栏目热点图片: pic_数字.gif 如:pic_001.gif
  说明:上、下、左、右 可以缩写为T、B、L、R
  CSS标准化设计命名
  1、类class的命名规范示例
  头:header
  内容:content/container
  尾:footer
  导航:nav
  侧栏:sidebar
  栏目:column
  页面外围控制整体布局宽度:wrapper
  左右中:left right center
  登录条:loginbar
  标志:logo
  广告:banner
  页面主体:main
  热点:hot
  新闻:news
  下载:download
  子导航:subnav
  菜单:menu
  子菜单:submenu
  搜索:search
  友情链接:friendlink
  页脚:footer
  版权:copyright
  滚动:scroll
  内容:content
  标签页:tab
  文章列表:list
  提示信息:msg
  小技巧:tips
  栏目标题:title
  加入:joinus
  指南:guild
  服务:service
  注册:regsiter
  状态:status
  投票:vote
  合作伙伴:partner
  2、注释的写法
  / Footer /
  内容区
  / End Footer /
  3、id的命名规范示例
  (1)页面结构
  容器: container
  页头:header
  内容:content/container
  页面主体:main
  页尾:footer
  导航:nav
  侧栏:sidebar
  栏目:column
  页面外围控制整体布局宽度:wrapper
  左右中:left right center
  (2)导航
  导航:nav
  主导航:mainbav
  子导航:subnav
  顶导航:topnav
  边导航:sidebar
  左导航:leftsidebar
  右导航:rightsidebar
  菜单:menu
  子菜单:submenu
  标题: title
  摘要: summary
  (3)功能
  标志:logo
  广告:banner
  登陆:login
  登录条:loginbar
  注册:regsiter
  搜索:search
  功能区:shop
  标题:title
  加入:joinus
  状态:status
  按钮:btn
  滚动:scroll
  标签页:tab
  文章列表:list
  提示信息:msg
  当前的: current
  小技巧:tips
  图标: icon
  注释:note
  指南:guild
  服务:service
  热点:hot
  新闻:news
  下载:download
  投票:vote
  合作伙伴:partner
  友情链接:link
  版权:copyright
  4、类class的书写规范示例
  (1)颜色:使用颜色的名称或者16进制代码,如
  .red { color: red; }
  .f60 { color: #f60; }
  .ff8600 { color: #ff8600; }
  (2)字体大小,直接使用”font+字体大小”作为名称,如
  .font12px { font-size: 12px; }
  .font9pt {font-size: 9pt; }
  (3)对齐样式,使用对齐目标的英文名称,如
  .left { float:left; }
  .bottom { float:bottom; }
  (4)标题栏样式,使用”类别+功能”的方式命名,如
  .barnews { }
  .barproduct { }
  5、CSS文件命名示例
  主要的 master.css
  模块 module.css
  基本共用 base.css
  布局,版面 layout.css
  主题 themes.css
  专栏 columns.css
  文字 font.css
  表单 forms.css
  补丁 mend.css
  打印 print.css
  6、注意事项
  (1)一律小写;
  (2)尽量用英文;
  (3)不加中杠和下划线;
  (4)尽量不缩写,除非一看就明白的单词
作者: 旋上的舞
链接:http://www.imooc.com/article/5219