要求:可读性和可测试

  • 优秀的代码不需要注释
  • 去掉不必要的代码
  • 一个代码不要使用2次
  • 提前return
  • 使用可检索的名称

    关于注释

  1. 函数本身就变成了注释。 这样的代码叫做自我描述

  2. 如果代码不清楚,需要注释,那么也许应该重写它。

    静态变量

    1. // 用 `var` 申明为大写的全局变量
    2. var MINUTES_IN_A_YEAR = 525600;
    3. for (var i = 0; i < MINUTES_IN_A_YEAR; i++) {
    4. runCronJob();
    5. }

    短路语法比条件语句更清晰

    函数参数少于2个,多了用对象

    1. // 多于3个参数传对象
    2. var menuConfig = {
    3. title: 'Foo',
    4. body: 'Bar',
    5. buttonText: 'Baz',
    6. cancellable: true
    7. }
    8. function createMenu(menuConfig) {
    9. ...
    10. }

    用 Object.assign 设置默认对象

    1. var menuConfig = {
    2. title: 'Order',
    3. // User did not include 'body' key
    4. buttonText: 'Send',
    5. cancellable: true
    6. }
    7. function createMenu(config) {
    8. config = Object.assign({
    9. title: 'Foo',
    10. body: 'Bar',
    11. buttonText: 'Baz',
    12. cancellable: true
    13. }, config);
    14. // 现在 config 等于: {title: "Foo", body: "Bar", buttonText: "Baz", cancellable: true}
    15. // ...
    16. }
    17. createMenu(menuConfig);

    一个函数只做一件事

    1. function emailClients(clients) {
    2. clients.forEach(client => {
    3. emailClientIfNeeded(client);
    4. });
    5. }
    6. function emailClientIfNeeded(client) {
    7. if (isClientActive(client)) {
    8. email(client);
    9. }
    10. }
    11. function isClientActive(client) {
    12. let clientRecord = database.lookup(client);
    13. return clientRecord.isActive();
    14. }

    jquery时代

    模块模式

    提供私有变量和函数,暴露出一个有限的API,其中包含返回对象的属性和方法

  1. // The module pattern
  2. var feature = (function() {
  3. // Private variables and functions
  4. var privateThing = "secret";
  5. var publicThing = "not secret";
  6. var changePrivateThing = function() {
  7. privateThing = "super secret";
  8. };
  9. var sayPrivateThing = function() {
  10. console.log( privateThing );
  11. changePrivateThing();
  12. };
  13. // Public API
  14. return {
  15. publicThing: publicThing,
  16. sayPrivateThing: sayPrivateThing
  17. };
  18. })();
  19. feature.publicThing; // "not secret"
  20. // Logs "secret" and changes the value of privateThing
  21. feature.sayPrivateThing();

使用对象字面量来封装代码

  1. // bad
  2. // 传统的jquery style
  3. $( document ).ready(function() {
  4. $( "#myFeature li" ).append( "" ).click(function() {
  5. var item = $( this );
  6. var div = item.find( "div" );
  7. div.load( "foo.php?item=" + item.attr( "id" ), function() {
  8. div.show();
  9. item.siblings().find( "div" ).hide();
  10. });
  11. });
  12. });
  13. // good
  14. // Using an object literal for a jQuery feature
  15. var myFeature = {
  16. init: function( settings ) {
  17. myFeature.config = {
  18. items: $( "#myFeature li" ),
  19. container: $( "
  20. " ),
  21. urlBase: "/foo.php?item="
  22. };
  23. // Allow overriding the default config
  24. $.extend( myFeature.config, settings );
  25. myFeature.setup();
  26. },
  27. setup: function() {
  28. myFeature.config.items
  29. .each( myFeature.createContainer )
  30. .click( myFeature.showItem );
  31. },
  32. createContainer: function() {
  33. var item = $( this );
  34. var container = myFeature.config.container
  35. .clone()
  36. .appendTo( item );
  37. item.data( "container", container );
  38. }
  39. $( document ).ready( myFeature.init );