簡體   English   中英

JSLint未使用的變量錯誤

[英]JSLint unused variable error

我創建了一個javascript文檔,並且希望此JSlint有效。

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驗證此錯誤時,出現以下錯誤:

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

我不知道該怎么解決。 使用uri變量, 'GET'是字符串常量,而不是變量。

所以我很困惑。

在您的代碼中,從182行開始,有一個函數傳遞給ajax.XHR.call ,該函數聲明一個從未使用的變量status

//                         |-- 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與Douglas Crockford的編碼風格有關非常重要,您認為它可能適合也可能不適合您自己的編碼風格。 您還可以查看JSHint ,它使您可以更好地控制選項。

未使用的變量:狀態182'uri',狀態203'GET'

意味着您在第182和203行上有未使用的變量status

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM