JSLint未使用的变量错误 [英] JSLint unused variable error

查看:103
本文介绍了JSLint未使用的变量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个javascript文档,我想让这个JSlint有效。

I created a javascript document and I want to make this JSlint valid.

http://pastebin.com/GvZLyNbV

/*jslint browser: true, indent: 2 */
/*global ActiveXObject: true, window: true*/
(function (window) {
  "use strict";
  /**
   * ajax class
   * 
   * ez az objektum fogja kezelni az ajax kérelmeket. lényege hogy nagyon 
   * minimális legyen. nem akarom jobban magyarázni, eléggé bonyolult, mert a
   * különböző böngészők különbözően kezelik az ajax hívásokat.
   */
  var ajax = window.ajax = {};

  ajax.XHR = {
    getXHR: function () {
      var http;
      try {
        http = new XMLHttpRequest();
        this.getXHR = function () {
          return new XMLHttpRequest();
        };
        return http;
      } catch (e) { }

      try {
        http = new ActiveXObject("MSXML2.XMLHTTP.3.0");
        this.getXHR = function () {
          return new ActiveXObject("MSXML2.XMLHTTP.3.0");
        };
        return http;
      } catch (f) { }

      try {
        http = new ActiveXObject("MSXML2.XMLHTTP");
        this.getXHR = function () {
          return new ActiveXObject("MSXML2.XMLHTTP");
        };
        return http;
      } catch (g) { }

      try {
        http = new ActiveXObject("Microsoft.XMLHTTP");
        this.getXHR = function () {
          return new ActiveXObject("Microsoft.XMLHTTP");
        };
        return http;
      } catch (h) { }
    },

    call: function (method, uri, callback, postData) {
      var xhr = this.getXHR();

      xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
          if (typeof callback === "function") {
            callback(xhr);
          }
        }
      };

      xhr.open(method, uri, true);

      if (method === "POST") {
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        //xhr.setRequestHeader("Content-length", postData.length);
        //xhr.setRequestHeader("Connection", "close");
        xhr.send(postData);
      } else {
        xhr.send(null);
      }
      return xhr;
    }
  };



  /////////////////////////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////////////////////
  /**
   * periodic object
   * 
   * Ez az osztály kezeli a periodikus hívások egyesítését. Hozzá tudunk adni 
   * listenereket
   * @see ajax.ListenerObj
   * 
   * Az a lényege hogy az összes listenert átnézi és egyesíti a requesteket
   * így egy kérelemben egybe elküldi az összes requestet.
   * 
   * @todo Jobban kellene rendezni az osztályt, ne csak így a levegőben lógjon,
   * meg majd bele kell venni a benchmark osztályt is meg jobban kell időzíteni.
   * 
   */

  ajax.periodic = {};


  ajax.periodic.isListen = false;
  ajax.periodic.timer = null;
  ajax.periodic.listenDelay = 5000;
  ajax.periodic.url = "ajax.php";
  ajax.periodic.listeners = [];

  /**
   * Ezzel a függvénnyel indítjuk el a periodikus hívásokat
   */
  ajax.periodic.startListen = function () {
    window.clearTimeout(ajax.periodic.timer);
    ajax.periodic.isListen = true;
    ajax.periodic.listen();
  };

  /**
   * Ezzel a fügvénnyel állítjuk le a periodikus hívásokat 
   */
  ajax.periodic.stopListen = function () {
    window.clearTimeout(ajax.periodic.timer);
    ajax.periodic.isListen = false;
  };

  /**
   * Ezzel a fügvénnyel adhatunk új ListenerObj objektumot a periodic osztályhoz
   * 
   * @see ajax.ListenerObj
   */
  ajax.periodic.addListener = function (obj) {
    if (obj instanceof ajax.ListenerObj) {
      ajax.periodic.listeners.push(obj);
    }
  };

  /**
   * Ezzel a fügvénnyel törölhetünk egy ListenerObj objektumot a periodic
   * osztályból
   * 
   * @see ajax.ListenerObj 
   */
  ajax.periodic.removeListener = function (obj) {
    var i = ajax.periodic.listeners.indexOf(obj);
    if (i >= 0) {
      ajax.periodic.listeners.splice(i, 1);
    }
  };

  /**
   * Ezzel a fügvénnyel kérdezzük le az össze ListenerObj objektum paramját
   * 
   * @param method : Ez "GET" vagy "POST" lehet
   * @param escape : Ez az hogy encodolja-e a tartalmat (default true)
   */
  ajax.periodic.getListenersQueries = function (method, escape) {
    var allQueries = new ajax.QueryCollection(),
      i;
    escape = (escape === false) ? false : true;
    for (i = 0; i < ajax.periodic.listeners.length; i += 1) {
      if (ajax.periodic.listeners[i].isListen()) {
        allQueries.mergeCollection(ajax.periodic.listeners[i].queries);
      }
    }

    return allQueries.queriesToString(method, escape);
  };

  /**
   * Ez a függvény meghíváskor elindul, és periodicc.delay időközönként
   * újrahívja magát. Az AJAX válaszban kapott adatot továbbítja a ListenerObj
   * objektumoknak.
   */
  ajax.periodic.listen = function () {
    if (!ajax.periodic.isListen) {
      return;
    }

    var startTime = (new Date()).getTime(),
      uri = ajax.periodic.url + "?" + ajax.periodic.getListenersQueries("get"),
      postData = ajax.periodic.getListenersQueries("post"),
      i;

    for (i = 0; i < ajax.periodic.listeners.length; i += 1) {
      ajax.periodic.listeners[i].setRequest(true);
    }

    ajax.XHR.call("POST", uri, function (data, status) {
      var i, delay;

      for (i = 0; i < ajax.periodic.listeners.length; i += 1) {
        if (ajax.periodic.listeners[i].isRequest() === true) {
          ajax.periodic.listeners[i].setRequest(false);
          ajax.periodic.listeners[i].parse(data);
        }
      }

      delay = (new Date()).getTime() - startTime;
      ajax.periodic.timer = window.setTimeout(ajax.periodic.listen, ((delay < ajax.periodic.listenDelay) ? ajax.periodic.listenDelay - delay : 0));
    }, postData);
  };

  /**
   * paraméterben megadott listener egyszer requesteli.
   */
  ajax.periodic.request = function (listener, func) {
    func = (typeof func === "function") ? func : function () {};

    ajax.XHR.call("POST", ajax.periodic.url + "?" + listener.queries.queriesToString("GET"), function (data, status) {
      func(data);
    }, ajax.listener.queries.queriesToString("POST"));
  };





  /////////////////////////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////////////////////

  /**
   * ajax.QueryCollection class
   * 
   * Ez az osztály a query paramétereket tárolja el.
   */
  ajax.QueryCollection = function () {
    var queryArray = [];

    this.queryArray = queryArray;


    /**
     * Query paraméter hozzáadása
     */
    this.addQuery = function (method, name, value) {
      if (!method || !name) {
        return this;
      }

      method = method.toString().toLowerCase();
      name = name.toString();
      value = (value === undefined) ? "" : value.toString();

      var i;
      for (i = 0; i < queryArray.length; i += 1) {
        if (queryArray[i].method === method && queryArray[i].name === name) {
          queryArray[i].value = value;
          return;
        }
      }
      //if not found then add a new
      queryArray.push({
        "method": method,
        "name": name,
        "value": value
      });
    };

    /**
     * Query paraméter törlése
     */
    this.removeQuery = function (method, name) {
      var i;
      for (i = 0; i < queryArray.length; i += 1) {
        if (queryArray[i].method === method && queryArray[i].name === name) {
          queryArray[i].splice(i, 1);
          return;
        }
      }
    };

    /**
     * Két ilyen osztály egyesítése
     */
    this.mergeCollection = function (obj) {
      if (!obj instanceof ajax.QueryCollection) {
        return;
      }
      var i;
      for (i = 0; i < obj.queryArray.length; i += 1) {
        this.addQuery(obj.queryArray[i].method, obj.queryArray[i].name, obj.queryArray[i].value);
      }
    };

    /*
     * Az argumentumban található osztály querijainak törlése ebből az osztályból
     */
    this.splitCollection = function (obj) {
      if (!obj instanceof ajax.QueryCollection) {
        return;
      }
      var i;
      for (i = 0; i < obj.queryArray.length; i += 1) {
        this.removeQuery(obj.queryArray[i].method, obj.queryArray[i].name);
      }
    };

    /*
     * Queryk egyesítése stringbe
     */
    this.queriesToString = function (method, escape) {
      var str = "",
        cnt = 0,
        i;

      escape = (escape === false) ? false : true;

      for (i = 0; i < queryArray.length; i += 1) {
        if (queryArray[i].method === method) {
          if (cnt !== 0) {
            str += "&";
          }

          cnt += 1;

          str += queryArray[i].name;

          if (queryArray[i].value !== "") {
            str += "=";

            if (escape === true) {
              str += encodeURIComponent(queryArray[i].value);
            }
            else {
              str += queryArray[i].value;
            }
          }
        }
      }
      return str;
    };
  };

  /*
   * ajax.ListenerObj class
   * 
   * Ez az osztály arra való hogy segítségvel csinálhatok listener objektumokat
   * amiknek beállíthatok queriket.
   */
  ajax.ListenerObj = function () {

    var isListen = false,
      isRequest = false,
      parserFunc = function () {};

    this.queries = new ajax.QueryCollection();

    this.setListen = function (a) {
      isListen = (a) ? true : false;
    };
    this.isListen = function () {
      return isListen;
    };

    this.setRequest = function (a) {
      isRequest = (a) ? true : false;
    };

    this.isRequest = function () {
      return isRequest;
    };

    this.setParser = function (a) {
      if (typeof a === "function") {
        parserFunc = a;
      }
      return this;
    };

    this.parse = function (data) {
      parserFunc(data);
    };
  };

}(window));

当我用JSLint验证时,我收到此错误:

When i validate this with JSLint, I get this error:

Error:
Unused variable: status 182 'uri', status 203 'GET'

我不明白应该修复什么。使用 uri 变量,'GET'是字符串常量,而不是变量。

I don't understand what should I fix. The uri variable is used, and the 'GET' is a string constant, not a variable.

所以我完全糊涂了。

推荐答案

在你的代码中,从第182行开始,你有一个函数传递到 ajax.XHR.call ,它声明了一个你永远不会使用的变量 status

In your code, starting on line 182, there's a function you pass into ajax.XHR.call which declares a variable, status, that you never use:

//                         |-- Function starts here
//                         V               V-- Variable (argument) `status`, never used
ajax.XHR.call("POST", uri, function (data, status) {
  var i, delay;

  for (i = 0; i < ajax.periodic.listeners.length; i += 1) {
    if (ajax.periodic.listeners[i].isRequest() === true) {
      ajax.periodic.listeners[i].setRequest(false);
      ajax.periodic.listeners[i].parse(data);
    }
  }

  delay = (new Date()).getTime() - startTime;
  ajax.periodic.timer = window.setTimeout(ajax.periodic.listen, ((delay < ajax.periodic.listenDelay) ? ajax.periodic.listenDelay - delay : 0));
}, postData);

您可以通过从函数签名中删除参数来消除错误。或者你可以忽略它,因为记住JSLint是关于道格拉斯克罗克福德的编码风格非常重要,这可能是你认为适合你自己的编码风格的,也可能不是。您还可以查看 JSHint ,它可以让您更好地控制选项。

You can get rid of the error by removing the argument from the function signature. Or you can ignore it, because it's very important to remember that JSLint is all about Douglas Crockford's coding style, which may or may not be what you believe to be appropriate for your own coding style. You might also look at JSHint, which gives you more control over options.

这篇关于JSLint未使用的变量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆