jQuery jslint var использовался до того, как был определен

0

Я использую 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, так что это может быть ropey, но я действительно хотел сделать это как можно лучше. Может ли кто-нибудь объяснить и предложить, почему я получаю это сообщение и как его избежать в будущем?

благодаря

  • 0
    Я не могу найти maxHeight в вашем коде
  • 0
    Хороший вопрос, я думаю, что он больше не используется. Я удалю это.
Теги:
jslint

1 ответ

2
Лучший ответ

Вы используете точку с запятой вместо запятой, когда объявляете несколько переменных с одним "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
  • 0
    Черт, это плохо с моей стороны. Спасибо чувак. Я приму ответ, когда это позволит мне

Ещё вопросы

Сообщество Overcoder
Наверх
Меню