类型
 
• 原始类型:我们可以直接使用值。
  ο  string
  ο  number
  ο  boolean
  ο  null
  ο  undefined
  1. var foo = 1,
  2. bar = foo;
  3.  
  4. bar = 9;
  5.  
  6. console.log(foo, bar); // => 1, 9

•   复合类型:我们通过`引用`对值进行间接访问。

  ο  object

  ο  array

  ο  function

  1. var foo = [1, 2],
  2. bar = foo;
  3.  
  4. bar[0] = 9;
  5.  
  6. console.log(foo[0], bar[0]); // => 9, 9

 

Objects
 
• 使用{}创建对象。
  1. // bad
  2. var item = new Object();
  3.  
  4. // good
  5. var item = {};

• 不要使用保留字作为关键字。

  1. // bad
  2. var superman = {
  3. class: 'superhero',
  4. default: { clark: 'kent' },
  5. private: true
  6. };
  7.  
  8. // good
  9. var superman = {
  10. klass: 'superhero',
  11. defaults: { clark: 'kent' },
  12. hidden: true
  13. };
Arrays
 

• 使用[]创建数组

  1. // bad
  2. var items = new Array();
  3.  
  4. // good
  5. var items = [];

• 如果你不知道数组长度,使用Array#push。

  1. var someStack = [];
  2.  
  3. // bad
  4. someStack[someStack.length] = 'abracadabra';
  5.  
  6. // good
  7. someStack.push('abracadabra');

• 当你需要复制数组的时候,请使用Array#slice。

  1. var len = items.length,
  2. itemsCopy = [],
  3. i;
  4.  
  5. // bad
  6. for (i = 0; i < len; i++) {
  7. itemsCopy[i] = items[i];
  8. }
  9.  
  10. // good
  11. itemsCopy = items.slice();
Strings

• 对于字符串,我们使用单引号''。

  1. // bad
  2. var name = "Bob Parr";
  3.  
  4. // good
  5. var name = 'Bob Parr';
  6.  
  7. // bad
  8. var fullName = "Bob " + this.lastName;
  9.  
  10. // good
  11. var fullName = 'Bob ' + this.lastName;

• 超过80个字符的字符串,我们使用串联符号(\),让字符串多行显示。

• 注意:如果过度使用带串联符号的字符可能会影响到性能。

 

  1. // bad
  2. var errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';
  3.  
  4. // bad
  5. var errorMessage = 'This is a super long error that \
  6. was thrown because of Batman. \
  7. When you stop to think about \
  8. how Batman had anything to do \
  9. with this, you would get nowhere \
  10. fast.';
  11.  
  12. // good
  13. var errorMessage = 'This is a super long error that ' +
  14. 'was thrown because of Batman.' +
  15. 'When you stop to think about ' +
  16. 'how Batman had anything to do ' +
  17. 'with this, you would get nowhere ' +
  18. 'fast.';

  

• 当我们在编程的时候,需要拼接出一个字符串,我们可以使用Array#join 代替字符串连接。尤其是对IE浏览器。

  1. var items,
  2. messages,
  3. length, i;
  4.  
  5. messages = [{
  6. state: 'success',
  7. message: 'This one worked.'
  8. },{
  9. state: 'success',
  10. message: 'This one worked as well.'
  11. },{
  12. state: 'error',
  13. message: 'This one did not work.'
  14. }];
  15.  
  16. length = messages.length;
  17.  
  18. // bad
  19. function inbox(messages) {
  20. items = '<ul>';
  21.  
  22. for (i = 0; i < length; i++) {
  23. items += '<li>' + messages[i].message + '</li>';
  24. }
  25.  
  26. return items + '</ul>';
  27. }
  28.  
  29. // good
  30. function inbox(messages) {
  31. items = [];
  32.  
  33. for (i = 0; i < length; i++) {
  34. items[i] = messages[i].message;
  35. }
  36.  
  37. return '<ul><li>' + items.join('</li><li>') + '</li></ul>';
  38. }
Functions

• 函数表达式

  1. // anonymous function expression
  2. var anonymous = function() {
  3. return true;
  4. };
  5.  
  6. // named function expression
  7. var named = function named() {
  8. return true;
  9. };
  10.  
  11. // immediately-invoked function expression (IIFE)
  12. (function() {
  13. console.log('Welcome to the Internet. Please follow me.');
  14. })();

• 绝对不要在非函数块(if,while)申明一个函数。我们可以把函数申明变成一个函数表达式。

  1. // bad
  2. if (currentUser) {
  3. function test() {
  4. console.log('Nope.');
  5. }
  6. }
  7.  
  8. // good
  9. if (currentUser) {
  10. var test = function test() {
  11. console.log('Yup.');
  12. };
  13. }

• 绝对不要把一个参数命名为arguments,arguments参数是函数作用域内给出的一个特殊变量,如果你把参数命名为arguments,那么这个参数就会覆盖它原有的特殊变量。

  1. // bad
  2. function nope(name, options, arguments) {
  3. // ...stuff...
  4. }
  5.  
  6. // good
  7. function yup(name, options, args) {
  8. // ...stuff...
  9. }

js 风格(注意事项)的更多相关文章

  1. 「标准」的 JS风格

    首先,这份 JS风格指南已经在我司的前端团队实行半年多了: 其次,在程序员的世界里,从入行到资深都需要面对几个世界级的难题,如: 世界上最好的编辑器是什么? 是用空格还是 TAB?用空格还特么衍生出 ...

  2. Vue.js 使用注意事项

    Vue.js 使用注意事项 1 过滤器主要用于简单的文本转换,如果要实现复杂的数据变换,应使用计算属性 指令的使用 v-bind基本用于HTML元素上的属性,如id.class.href.src等 v ...

  3. soui使用wke时,设置js回调注意事项

    wke响应网页js函数调用时注意: 必须等网页加载完成后,才能通过SetJsFunc设置js函数与c++回调的对应.网页未加载就设置,不会响应c++函数. 示例代码: wkeJSData* data ...

  4. js兼容注意事项--仅供参考

    做BS开发就难免会用到javascript,而每个浏览器对javascript的支持有不同.这就需要我们程序员去兼容他们,不然有些浏览器就无法运行我们的代码.就会造来客户的投诉,如果让BoSS知道了, ...

  5. JS中注意事项

    (一)判断中注意事项 一.所有的相对路径都别拿来做判断 1.img src='...' 2.href='1.css', href='html/index.html' 3.img src='http:/ ...

  6. 关于HTML中标签<a>使用js的注意事项

    以下两点都不可取: 1.<a href="#" onClick="popUp('http://www.baidu.com');return false;" ...

  7. 最近兰州的js风格写个插件和一个template engine

    /* *@Product Name: Rational Framework Author: Calos Description: pager !important: pager */ (functio ...

  8. js风格技巧

    1.一个页面的所有js都可以写成这样,比如:   var index ={};   index.User = ****;   index.Init = function(){ $("$tes ...

  9. js操作注意事项

    1.函数赋值给变量时,不能加括号 function fun() { ... } var str=fun; 2.js创建构造函数和调用对象,对象内不能用var 变量,只能用this function f ...

随机推荐

  1. Adb分析及获取root权限

    Adb的全称为Android Debug Bridge,起到通过PC对Android系统的调试桥的作用,是一个多用途的工具,它能够执行多种命令,还能提供一个shell.这儿简单介绍一下Adb的代码结构 ...

  2. poj 1274 The Perfact Stall

    click here ~~ ***The Perfect Stall*** Description Farmer John completed his new barn just last week, ...

  3. C语言之基本算法08—去掉最高分去掉最低分求平均值

    // /* ================================================================== 题目:选拔赛中通经常使用这种办法求选手分数,去掉一个最 ...

  4. css hacks

    /***** Selector Hacks ******/ /* IE6 and below */ * html #uno { color: red } /* IE7 */ *:first-child ...

  5. 关于wcf,webservice,webapi或者其他服务或者接口有什么区别 WCF、WebAPI、WebService之间的区别 【转载】HTTP和SOAP完全就是两个不同的协议 WebService学习总结(一)——WebService的相关概念

    wcf,webservice采用的是rpc协议,这个协议很复杂,所以每次要传递.要校验的内容也很复杂,别看我们用的很简单,但实际是frame帮我们做掉了rpc生成.解析的事情webapi遵循是rest ...

  6. VS2012安装git

    新安装了vs2012,尝试使用Git,安装过程比较简单,记录下来以备以后查阅.   1. 下载.安装Git 我的系统是Windows 7,需要安装Git for Windows. 下载地址: http ...

  7. Chisel Tutorial(六)——port

    下面内容根据2015-7-10版的Chisel 2.2 Tutorial整理 port就是硬件单元对外的接口,须要指明方向(输入还是输出).一个port声明的样例例如以下: class Decoupl ...

  8. Zip 压缩问题件,获取真实扩展名

    ].InputStream); foreach (var element in _ZIP.Entries) { using (System.IO.StreamReader _ddd = new Sys ...

  9. MySQL 记录中包含换行符

    换行符: \r\n \r CHAR(10) 处理方法: REPLACE(str,from_str,to_str); 如:REPLACE('aaa\nbbb','\n','');

  10. Jquery学习笔记(9)--注册验证复习(未用到ajax)

    纯复习,在$(this).val()这里浪费了时间,val()只适合input里面的value值,如果是span等标签里包裹的文本要用text()!! <!DOCTYPE html> &l ...