• 推荐编码规范
    • 命名规范
    • 语法规范
    • 书写规范
    • 参考

    推荐编码规范

    下面是 Cocos Creator 开发团队使用的编码规范,收录在手册里以供游戏开发者和工具开发者参考。

    命名规范

    • 当我们为变量, 函数和实例命名时, 使用 camelCase 命名法.

      1. // bad
      2. var FOOBar = {};
      3. var foo_bar = {};
      4. function FOOBar () {}
      5. // good
      6. var fooBar = {};
      7. function fooBar () {}
    • 当我们为类或者模块命名时, 使用 PascalCase 命名法.

      1. // bad
      2. var foobar = cc.Class({
      3. foo: 'foo',
      4. bar: 'bar',
      5. });
      6. var foobar = require('foo-bar');
      7. // good
      8. var FooBar = cc.Class({
      9. foo: 'foo',
      10. bar: 'bar',
      11. });
      12. var FooBar = require('foo-bar');
    • 使用前置下划线 _ 当我们为私有属性命名

      1. // bad
      2. this.__firstName__ = 'foobar';
      3. this.firstName_ = 'foobar';
      4. // good
      5. this._firstName = 'foobar';
    • 文件名我们采用 dash 命名法

      1. // bad
      2. fooBar.js
      3. FooBar.js
      4. // good
      5. foo-bar.js

    语法规范

    • 使用 {} 创建一个 object

      1. // bad
      2. var obj = new Object();
      3. // good
      4. var obj = {};
    • 使用 [] 创建一个 array

      1. // bad
      2. var array = new Array();
      3. // good
      4. var array = [];
    • 尽可能在 js 代码中使用单引号 '' 来定义 string

      1. // bad
      2. var str = "Hello World";
      3. // good
      4. var str = 'Hello World';
    • 多行 string 定义时, 尽可能使用 + 定义

      1. // bad
      2. const 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. // bad
      4. const errorMessage = 'This is a super long error that was thrown because \
      5. of Batman. When you stop to think about how Batman had anything to do \
      6. with this, you would get nowhere \
      7. fast.';
      8. // good
      9. const errorMessage = 'This is a super long error that was thrown because ' +
      10. 'of Batman. When you stop to think about how Batman had anything to do ' +
      11. 'with this, you would get nowhere fast.';
    • 使用 ===!== 而不是 ==!=.

    书写规范

    • 根据个人习惯, 和原代码作者格式, 选择 4 个空格或者 2 个空格作为缩进

      1. // bad
      2. function() {
      3. var name;
      4. }
      5. // very bad
      6. function() {
      7. ∙∙<tab>∙∙var name;
      8. }
      9. // good
      10. function() {
      11. ∙∙var name;
      12. }
      13. // good
      14. function() {
      15. ∙∙∙∙var name;
      16. }
    • 尽可能将 { 和表达式放在同一行

      1. // bad
      2. if ( isFoobar )
      3. {
      4. }
      5. // good
      6. if ( isFoobar ) {
      7. }
      8. // bad
      9. function foobar()
      10. {
      11. }
      12. // good
      13. function foobar() {
      14. }
      15. // bad
      16. var obj =
      17. {
      18. foo: 'foo',
      19. bar: 'bar',
      20. }
      21. // good
      22. var obj = {
      23. foo: 'foo',
      24. bar: 'bar',
      25. }
    • { 前请空一格

      1. // bad
      2. function test(){
      3. console.log('test');
      4. }
      5. // good
      6. function test() {
      7. console.log('test');
      8. }
      9. // bad
      10. dog.set('attr',{
      11. age: '1 year',
      12. breed: 'Bernese Mountain Dog',
      13. });
      14. // good
      15. dog.set('attr', {
      16. age: '1 year',
      17. breed: 'Bernese Mountain Dog',
      18. });
    • 在逻辑状态表达式 ( if, while ) 的 ( 前请空一格

      1. // bad
      2. if(isJedi) {
      3. fight ();
      4. }
      5. // good
      6. if (isJedi) {
      7. fight();
      8. }
    • operator 之间请空一格

      1. // bad
      2. var x=y+5;
      3. // good
      4. var x = y + 5;
    • 在 Block 定义之间请空一行

      1. // bad
      2. if (foo) {
      3. return bar;
      4. }
      5. return baz;
      6. // good
      7. if (foo) {
      8. return bar;
      9. }
      10. return baz;
      11. // bad
      12. const obj = {
      13. foo() {
      14. },
      15. bar() {
      16. },
      17. };
      18. return obj;
      19. // good
      20. const obj = {
      21. foo() {
      22. },
      23. bar() {
      24. },
      25. };
      26. return obj;
      27. // bad
      28. const arr = [
      29. function foo() {
      30. },
      31. function bar() {
      32. },
      33. ];
      34. return arr;
      35. // good
      36. const arr = [
      37. function foo() {
      38. },
      39. function bar() {
      40. },
      41. ];
      42. return arr;
    • 不要使用前置逗号定义

      1. // bad
      2. var story = [
      3. once
      4. , upon
      5. , aTime
      6. ];
      7. // good
      8. var story = [
      9. once,
      10. upon,
      11. aTime,
      12. ];
      13. // bad
      14. var hero = {
      15. firstName: 'Ada'
      16. , lastName: 'Lovelace'
      17. , birthYear: 1815
      18. , superPower: 'computers'
      19. };
      20. // good
      21. var hero = {
      22. firstName: 'Ada',
      23. lastName: 'Lovelace',
      24. birthYear: 1815,
      25. superPower: 'computers',
      26. };

    参考

    airbnb/es5