【问题标题】:jQuery jslint var was used before it was defined在定义之前使用了 jQuery jslint var
【发布时间】:2013-12-18 03:21:06
【问题描述】:

我一直在使用 jslint 来尝试查看它对我的代码的说明,我得到了很多标志,但我正在努力改进它。但是我被错误困住了

在定义之前使用了maxHeight

我的 jQuery:

$.fn.thumbSizr = function () { // begin function
    "use strict";
    return this.each(function () {
        var $this = $(this);
            maxWidth = $(this).parent().width(); // Max width for the image
            minHeight = $(this).parent().height();    // Max height for the image
            ratio = 0;  // Used for aspect ratio
            width = $(this).width();    // Current image width
            height = $(this).height();  // Current image height

        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = width * ratio;    // Reset width to match scaled image
        }

        // Check if current height is larger than max
        if(height < minHeight){
            ratio = minHeight / height; // get ratio for scaling image
            $(this).css("height", minHeight);   // Set new height
            $(this).css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;    // Reset width to match scaled image
        }

        var $img = $(this),
        css = {
            position: 'absolute',
            marginLeft: '-' + (parseInt( $img.css('width') ) / 2) + 'px',
            left: '50%',
            top: '50%',
            marginTop: '-' + (parseInt( $img.css('height') ) / 2) + 'px'
        };

        $img.css( css );
   });
};

我不是 jQuery 专业人士,所以这可能有点啰嗦,但我真的想让它尽可能好。谁能解释并建议我为什么收到此消息以及将来如何避免它?

谢谢

【问题讨论】:

  • 我似乎在你的代码中找不到maxHeight
  • 好点,我认为它不再使用了。我会删除它。

标签: javascript jquery jslint


【解决方案1】:

使用单个“var”声明多个变量时,您使用的是分号而不是逗号

这部分错了:

 var $this = $(this);
     maxWidth = $(this).parent().width(); // Max width for the image
     minHeight = $(this).parent().height();    // Max height for the image
     ratio = 0;  // Used for aspect ratio
     width = $(this).width();    // Current image width
     height = $(this).height();  // Current image height

固定:

 var $this = $(this),
     maxWidth = $(this).parent().width(), // Max width for the image
     minHeight = $(this).parent().height(),    // Max height for the image
     ratio = 0,  // Used for aspect ratio
     width = $(this).width(),    // Current image width
     height = $(this).height();  // Current image height

【讨论】:

  • 该死,这对我不利。谢啦。当它允许我时,我会接受答案
猜你喜欢
  • 2013-08-17
  • 2017-07-18
  • 2012-03-26
  • 2011-11-06
  • 2012-10-09
  • 2014-06-07
  • 1970-01-01
相关资源
最近更新 更多