简体   繁体   中英

Incorrect JSHint warning “variable is defined but never used.”

I have the following JS code:

var func = function (dimension) {
  var element,
      minSize = "min-" + dimension,
      maxSize = "max-" + dimension;           

  element = jQuery('<div>');
  element.css({
    maxSize: "",
    minSize: ""
  });

};

func();

JSHint throws me the following warnings:

Two unused variables

3 minSize

4 maxSize

Why? It is obvious that I'm using them.

The variables are assigned but not used.

Assignment:

minSize = "min-" + dimension, maxSize = "max-" + dimension;

Not using minSize or maxSize but creating two properties with the same names on an anonymous object :

element.css({ maxSize: "", minSize: "" });

you havn't use the variable. you can't use literal object with variable as key. try

var func = function (dimension) {
  var element,
      minSize = "min-" + dimension,
      maxSize = "max-" + dimension;           

  element = jQuery('<div>');
  var obj={};
  obj[maxSize]=""
  obj[minSize]=""

  element.css(obj);

};

func();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM