【问题标题】:get rid of redundant code?摆脱冗余代码?
【发布时间】:2016-09-29 08:14:44
【问题描述】:

由于代码气候问题,我需要清理我的代码。这个jquery太相似了。我该如何解决这个问题?

  var angle = 0;
  $('.rotate-receipt').on('click', function () {
      var index = $(this).data('button-index');
      angle = (angle + 90)%360;
      var className = 'rotate' + angle;
      $('#receipt-image-'+index).removeClass().addClass(className);
  });

  $('.zoom-in').on('click', function () {
      var index = $(this).data('button-index');
      var image = $('#receipt-image-'+index);
      image.width(image.width() + 100);
  });

  $('.zoom-out').on('click', function () {
      var index = $(this).data('button-index');
      var image = $('#receipt-image-'+index);
      image.width(image.width() - 100);
  });
});

【问题讨论】:

标签: javascript jquery optimization


【解决方案1】:

放大和缩小你可以试试这个:

$('.zoom-in,.zoom-out').on('click', function() {
      var index = $(this).data('button-index');
      var image = $('#receipt-image-'+index);
      if ($(this).hasClass('zoom-out')) {
         image.width(image.width() - 100);
      }
      else {
         image.width(image.width() + 100);
      }
});

【讨论】:

    【解决方案2】:
    var angle = 0;
    $('.rotate-receipt,.zoom-in,.zoom-out').on('click', function () {
      var index = $(this).data('button-index');
      var image = $('#receipt-image-'+index);
    
      if($(this).hasClass("rotate-receipt")) {
        angle = (angle + 90)%360;
        var className = 'rotate' + angle;
        $('#receipt-image-'+index).removeClass().addClass(className);
      }
      else if($(this).hasClass("zoom-in")) {
        image.width(image.width() + 100);
      }
      else if($(this).hasClass("zoom-out")) {
        image.width(image.width() - 100);
      }
    

    });

    希望对你有用:)

    【讨论】:

      【解决方案3】:

      还不如投入我的 2 美分...

      var angle = 0;
      $('.rotate-receipt').on('click', function () {
        transform( this, 'rotate-receipt' );
      });
      
      $('.zoom-in').on('click', function () {
        transform( this, 'zoom-in' );
      });
      
      $('.zoom-out').on('click', function () {
        transform( this, 'zoom-out' );
      });
      
      function transform( el, type ) {
      
        var index = $(el).data('button-index');
        var image = $('#receipt-image-'+index);
      
        switch( type ) {
      
          case 'rotate-receipt':
            angle = (angle + 90)%360;
            var className = 'rotate' + angle;
            $('#receipt-image-'+index).removeClass().addClass(className);
            break;
      
          case 'zoom-in':
            image.width(image.width() + 100);
            break;
      
          case 'zoom-out':
            image.width(image.width() - 100);
            break;
      
        }
      
      }
      

      【讨论】:

        【解决方案4】:
            $('.rotate-receipt, .zoom-in, .zoom-out').on('click' , function() {
                var index = $(this).data('button-index');
                if ($(this).hasClass('rotate-receipt')) {
                    angle = (angle + 90)%360;
                    var className = 'rotate' + angle;
                    $('#receipt-image-'+index).removeClass().addClass(className);
                } else {
                    var image = $('#receipt-image-'+index);
                    var toAdd = $(this).hasClass('zoom-out') ? - 100 : 100;
                    image.width(image.width() + toAdd);
                }
            })
        

        【讨论】:

          【解决方案5】:

          您可以将放大和缩小的代码减少为一个功能。代码如下:

          $('.zoom-in').on('click', zoom_in_out(100));
          $('.zoom-out').on('click', zoom_in_out(-100));
          
          function zoom_in_out(zoom_value) {
            var index = $(this).data('button-index');
            var image = $('#receipt-image-'+index);
            image.width(image.width() + zoom_value);
          });
          

          您应该尽量减少代码,不要重复自己。这是一种更好的方法,不仅因为它的代码更少,而且因为类似的功能被放置在一个位置。这样,如果您需要包含其他一些常见操作,您只需要在一个地方添加即可。它更多的是关于更好的编码实践,而不仅仅是减少代码行数。

          【讨论】:

            猜你喜欢
            • 2017-11-22
            • 1970-01-01
            • 2011-03-09
            • 2011-10-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多