空行

以下几种情况需要空行:

  • 变量声明后(当变量声明在代码块的最后一行时,则无需空行)
  • 注释前(当注释在代码块的第一行时,则无需空行)
  • 代码块后(在函数调用、数组、对象中则无需空行)
  • 文件最后保留一个空行
  1. // need blank line after variable declaration
  2. var x = 1;
  3. // not need blank line when variable declaration is last expression in the current block
  4. if (x >= 1) {
  5. var y = x + 1;
  6. }
  7. var a = 2;
  8. // need blank line before line comment
  9. a++;
  10. function b() {
  11. // not need blank line when comment is first line of block
  12. return a;
  13. }
  14. // need blank line after blocks
  15. for (var i = 0; i < 2; i++) {
  16. if (true) {
  17. return false;
  18. }
  19. continue;
  20. }
  21. var obj = {
  22. foo: function() {
  23. return 1;
  24. },
  25. bar: function() {
  26. return 2;
  27. }
  28. };
  29. // not need blank line when in argument list, array, object
  30. func(
  31. 2,
  32. function() {
  33. a++;
  34. },
  35. 3
  36. );
  37. var foo = [
  38. 2,
  39. function() {
  40. a++;
  41. },
  42. 3
  43. ];
  44. var foo = {
  45. a: 2,
  46. b: function() {
  47. a++;
  48. },
  49. c: 3
  50. };