如何修复这些 JSLint 错误?

How do I fix these JSLint errors?

我正在尝试根据 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");
    }
};

谁能告诉我为什么这些是错误的以及我应该如何解决它们?我用谷歌搜索了更多信息,但实际上找不到任何有用的信息。我试过将 "new" 添加到 SortPosts 但它给了我消息 Do not use 'new' for side effects.

要解决这些问题,请将第 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

Missing 'new'.
SortPosts(categories[categoryNumber].textContent);

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

Unexpected '('.
if (typeof (Storage) !== "undefined") {

typeof 是运算符,不是函数。

使用 typeof Storage 而不是 typeof (Storage)

line 65 character 9Unexpected 'typeof'. Use '===' to compare directly with undefined.

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

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