【问题标题】:How do I fix these JSLint errors?如何修复这些 JSLint 错误?
【发布时间】:2015-04-23 08:40:54
【问题描述】:

我正在尝试根据 JSLint 修复一些错误,但有些错误我无法摆脱:

line 46 character 13  
Missing 'new'.  
     SortPosts(categories[categoryNumber].textContent);

line 65 character 16  
Unexpected '('.
    if (typeof (Storage) !== "undefined") {

line 65 character 9Unexpected 'typeof'. Use '===' to compare directly with undefined.
    if (typeof (Storage) !== "undefined") {

这是我的代码:

window.onload = function () {
    "use strict";
    var categories,
        value,
        i,
        j,
        article,
        li,
        postsAvailable = false;
    function removeElementsByClass() {
        var elements = document.getElementsByClassName("forumPost");
        while (elements.length > 0) {
            elements[0].parentNode.removeChild(elements[0]);
        }
    }
    function addRow(input) {
        article = document.createElement('article');
        article.className = 'forumPost';
        article.innerHTML = "<div id='imageDiv'><img src='" + input.avatar + "' alt='Avatar'id='imageHTML'/><div id='personDiv'> " + input.firstName + " " + input.lastName + " (" + input.age + ") </div><div id='workDiv'>" + input.work + " </div></div><div id='headerDiv'>" + input.firstName + " schreef om <div id='dateDiv'>" + input.date + "</div></div></br><div id='postDiv'>" + input.post + "</div></article>";
        document.getElementById('content').appendChild(article);
    }
    function SortPosts(category) {
        if (localStorage.getItem(category) === "") {
            alert("No posts found!");
        } else {
            removeElementsByClass();
            if (category !== "Algemeen") {
                value = JSON.parse(localStorage.getItem(category));
                for (i = 0; i < value.length; ++i) {
                    addRow(value[i]);
                }
            } else {
                for (i = 0; i < localStorage.length; ++i) {
                    if (localStorage.getItem(localStorage.key(i)) !== "") {
                        value = JSON.parse(localStorage.getItem(localStorage.key(i)));
                        for (j = 0; j < value.length; ++j) {
                            addRow(value[j]);
                        }
                    }
                }
            }
        }
    }
    function getPostSorter(categoryNumber) {
        return function () {
            SortPosts(categories[categoryNumber].textContent);
        };
    }
    document.getElementById("newCategoryButton").onclick = function () {
        var input = document.getElementById("newCategoryInput").value;
        if (input !== "") {
            if (localStorage.getItem(input) !== null) {
                alert("Categorie bestaat al!");
            } else {
                li = document.createElement('li');
                li.innerHTML = "<a href='#'>" + input + "</a></li>";
                localStorage.setItem(input, "");
                document.getElementById("categories").appendChild(li);
            }
        } else {
            alert("Uw invoer mag niet leeg zijn!");
        }
        document.getElementById("newCategoryInput").value = "";
    };
    if (typeof (Storage) !== "undefined") {
        for (i = 0; i < localStorage.length; ++i) {
            if (localStorage.getItem(localStorage.key(i)) !== "") {
                postsAvailable = true;
                break;
            }
        }
        if (postsAvailable === false) {
            article = document.createElement('article');
            article.className = 'infobox';
            article.innerHTML = "<header><h2>Geen posts gevonden!</h2></header><section><p>Ga naar het forum toe om een onderwerp toe te voegen</p></section></article>";
            document.getElementById('content').appendChild(article);
        } else {
            for (i = 0; i < localStorage.length; ++i) {
                if (localStorage.getItem(localStorage.key(i)) !== "") {
                    value = JSON.parse(localStorage.getItem(localStorage.key(i)));
                    for (j = 0; j < value.length; ++j) {
                        addRow(value[j]);
                    }
                }
            }
        }
        for (i = 0; i < localStorage.length; ++i) {
            li = document.createElement('li');
            li.className = "category";
            li.innerHTML = "<a href='#'>" + localStorage.key(i) + "</a></li>";
            document.getElementById("categories").appendChild(li);
        }
        categories = document.getElementsByClassName("category");
        for (i = 0; i < categories.length; ++i) {
            categories[i].addEventListener("click", getPostSorter(i));
        }
    } else {
        alert("Uw browser ondersteunt geen localStorage. Gebruik alstublieft een nieuwere browser zoals Google Chrome, Mozilla Firefox of > Internet Explorer 7");
    }
};

谁能告诉我为什么这些是错误的,我应该怎么做才能解决它们?我用谷歌搜索了更多信息,但实际上找不到任何有用的信息。我尝试在 SortPosts 中添加“新”,但它给了我消息 Do not use 'new' for side effects.

【问题讨论】:

    标签: javascript jslint


    【解决方案1】:

    缺少“新”。
    SortPosts(categories[categoryNumber].textContent);

    按照惯例,名称以大写字母开头的函数是构造函数。你的不是。将函数重命名为sortPosts

    意外的'('。
    if (typeof (Storage) !== "undefined") {

    typeof 是一个运算符,而不是一个函数。

    使用typeof Storage 而不是typeof (Storage)

    第 65 行字符 9 意外的 'typeof'。使用 '===' 直接与 undefined 进行比较。

    不要使用typeof。使用Storage !== undefined

    注意:JSLint 是一个非常固执己见的 linter。 undefined 有可能被覆盖。我更喜欢typeof 方法。

    【讨论】:

      【解决方案2】:

      要解决这些问题,请将第 45 行更改为:

              newSortPosts(categories[categoryNumber].textContent);
      

      在您的代码中将 SortPosts 重命名为 newSortPosts。

      在第 65 行,您无需比较 typeof,而只需查看 Storage 是否未定义:

      if (Storage !== undefined) {
      

      在使用typeof时请注意,不需要括号,因为typeof是运算符:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

      【讨论】:

      • 尝试使用new 的属性会抛出Uncaught SyntaxError: Unexpected token .
      • 测试变量是否等于 string undefined 不会告诉你它是否未定义。
      • 那么sortPosts 你要么必须将其重命名为newSortPosts 要么只是sortPosts
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 2018-11-25
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多