编程语言
首页 > 编程语言> > javascript – 如何在JSLINT中按方法处理未使用的变量?

javascript – 如何在JSLINT中按方法处理未使用的变量?

作者:互联网

我有一组方法,我通过变量名调用,如下所示:

var some = factory[some_method](a, b, c);

我的问题是我调用的一些方法需要a,b,c而有些只需要a或a,b.没问题,但JSLINT抱怨未使用的a / b.

题:
虽然在方法特定范围内未使用但我如何保留变量?我不想设置全局未使用:true?它也可以在本地为单一方法设置吗?

谢谢!

编辑:
我正在调用上面这样的方法:

// content_dict = determines which method to call (along with properties being set)
app.setContent = function (content_dict, url_dict, create, purge) {
    var i, skip, container, target, pass, spec, dependency;

    spec = url_dict || {};

    switch (content_dict.generate) {

    case "widget":
      return factory[content_dict.type](content_dict, spec, create);
    ...

content_dict和方法调用的示例:

  //      {
  //        "generate": "widget",
  //        "type": "navbar",
  //        "property_dict": {
  //           "foo":"bar"
  //        }
  //      }
  factory.navbar = function (content_dict) {
    var navbar, controls, i, element, target, order;

    if (content_dict === undefined) {
      util.error({
        "error": "Generate Navbar: Missing Configuration"
      });
    } else {

      // navbar
      navbar = factory.element(
        "div",
        {"className": "navbar ui-navbar " + (content_dict.class_list || "")},
        {
          "data-role": "navbar",
          "role": "navigation",
          "data-enhanced": "true"
        },
        {"data-reference": content_dict.reference || null}
      );

      // controls
      controls = factory.element(
        "ul",
       {
          "className": "ui-grid-" +
            util.toLetter(content_dict.children.length - 1).toLowerCase()
        }
      );

      // children
      if (spec.children) {
        for (i = 0; i < content_dict.children.length; i += 1) {
          element = content_dict.children[i];
          target = factory.element(
            "li",
            {"className": "ui-block-" + util.toLetter(i + 1).toLowerCase()}
          );

          // class string
          // TODO: needed?
          if ((element.type !== "input" && element.type !== "select") &&
              element.direct) {
            order = i === 0 ? " ui-first-child " :
                (i === (content_dict.length - 1) ? " ui-last-child " : " ");

            element.direct.className = (element.direct.className || "") +
                order + (element.type === "a" ? (" ui-btn ui-shadow " +
                    factory.generateIconClassString(element)) : " ");
          }

          target.appendChild(app.setContent(element));
          controls.appendChild(target);
        }
      }

      // assemble
      navbar.appendChild(controls);

      return navbar;
    }
  };

因此,上面的方法不需要设置url_dict,而这个方法可以:

  //      {
  //        "generate": "widget",
  //        "type": "page",
  //        "property_dict": {
  //           "foo":"bar"
  //        }
  //      }
  factory.page = function (content_dict, url_dict, create) {
    var i, j, last, wrapper, split_url, promises, container, target,
      view, render;

    container = document.getElementById(url_dict.id);
    target = document.createDocumentFragment();
    view = (url_dict && url_dict.mode) ? url_dict.mode : "default";
    render = content_dict.layout[view];

    if (render) {
      promises = [];
      for (i = 0; i < render.length; i += 1) {
        promises[i] = app.setContent(render[i], url_dict, create);
      }
    } else {
      util.error({"error": "factory.page: Missing view"});
    }

    return RSVP.all(promises)
     .then(function (promises) {
        for (j = 0; j < promises.length; j += 1) {
         target.appendChild(promises[j]);
        }

        if (container === null || create === true) {
          last = document.body.lastChild;
          wrapper = factory.element("div", {"className": "ui-content"}, {});
          wrapper.setAttribute("data-bound", true);
          split_url = url_dict.url.split("#");

          container = factory.element(
           "div",
           {
             "id": url_dict.id,
             "className": " ui-page " + ("ui-page-theme-" +
                content_dict.theme || "") + " " + ((content_dict.fix &&
                    content_dict.fix === false) ? "" :
                        " ui-page-header-fixed ui-page-footer-fixed")
           },
           {
             "data-module": url_dict.id,
             "data-role": "page",
             "data-url": split_url[1] ? split_url[1].split("?")[0] :
                split_url[0],
             "data-external-page": true,
             "tabindex": 0,
             "data-enhanced": true
          }
        );

        wrapper.appendChild(app.breadcrumb(url_dict));
        wrapper.appendChild(target);
        container.appendChild(wrapper);

        if (util.testForString("ui-footer", last.className)) {
          document.body.insertBefore(container, last);
        } else {
          document.body.appendChild(container);
        }

        $(document).enhanceWithin();

        // trigger new transition to this page
        $.mobile.changePage("#" + url_dict.id);

      } else {
        factory.util.updatePageSection(url_dict.id, target);

        // also changePage in case we are not on the active
        // page (this will only happen deeplink > home
        if (url_dict.id !== util.getActivePage()) {
          $.mobile.changePage("#" + url_dict.id);
        }
      }

      // update header
      if (create !== false) {
        app.setPageTitle(
          content_dict.title_i18n,
          target.querySelectorAll("div.ui-header")
        );
      }
    })
    .fail(util.error);
};

为了完整起见,有两个例子.我想我可以随机播放我的参数并最后传递url_dict,这意味着我的方法调用会发生很多变化.那么,仍然存在问题是否有办法在每个方法的基础上设置JSLINT规则?

谢谢.

解决方法:

你可以随时打开和关闭unparam.

/*jslint white:true, browser:true, sloppy: true */
function test1(p1)  {
    // param check on, no error
    window.alert(p1); 
}

/*jslint unparam: true */
function test2(p1, p2, p3)  {
    // param check off temporarily, two unused, no error
    window.alert(p2);
}
/*jslint unparam: false */

function test3(p1, p2, p3)  {
    // param check back on, one unused, error reported.
    window.alert(p3 + p2);
}

虽然除非你担心在其他地方捕获未使用的params,但最好将unparam放在你的第一个jslint行中.

/ * jslint white:true,browser:true,sloppy:true,unparam:true * /

请记住,如果/ * jslint unparam:false * /在文件中的任何位置,它将重新打开以获取文件的余额.第一个jslint设置不是全局的或类似的东西.

标签:javascript,jslint,unused-variables
来源: https://codeday.me/bug/20190708/1406740.html