繁体   English   中英

JSHint和JSLint未捕获的未定义函数

[英]Undefined function uncaught by JSHint and JSLint

最近,我一直在使用JavaScript为我的网站开发Cookie系统。 现在,当我将它们放在一起时,它们拒绝在Chrome的JavaScript控制台中运行。

它说TypeError: undefined is not a function尽管一切似乎都已正确定义。

我已经通过JSLint和JSHint运行了它们,但它们没有发现任何错误。

    //Date finder
    var day = new Date();
    day = day.substring(4, 100);

    //Pause function for later use
    function sleep(ms) {
        var currentTime = new Date().getTime();
        while (currentTime + ms >= new Date().getTime()) {
            alert("Please be patient while we connect...");
        }
    }

    //Sets cookie with cookie name, cookie value, and cookie expiry (converted into milliseconds by function)
    function setCookie(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays*24*60*60*1000));
        var expires = "expires="+d.toUTCString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
    }

    //Uses a while loop to search through cookies outputed by "document.cookie" and find desired cookie
    function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for(var i=0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') {
                c = c.substring(1);
            }
            if (c.indexOf(name) != -1) {
                return c.substring(name.length, c.length);
            }
        }
        return "";
    }

    //Option to delete cookies if client desires
    function deleteCookie(del) {
        if (del == "Y") {
            var checkDel = confirm("Are you sure? Your username will be lost.");
            if (checkDel === false) {
                document.cookie = "username=; day";
            }
        }
    }

    //User input area - either uses cookie to identify user or requests user to input value
    function checkCookie() {
        var user = getCookie("username");
        if (user !== "") {
            alert("Welcome again " + user);
            var clearCookie = prompt("Do you wish to remove your cookie from this site?", "Y or N").toUpperCase();
            deleteCookie(clearCookie);
        } else {
            user = prompt("Please enter your name:").toUpperCase(0,1);
            if (user !== "" && user !== null) {
                setCookie("username", user, 91);
            }
        }
    }
    alert("Please wait while we connect you to our servers.\nPress ENTER to initiate.");
    console.log("Logging onto system...");
    sleep(3800);
    console.log("User request: " + Math.floor(Math.random()*10000) + "; Server received request;        Connection accepted; " + day);
    checkCookie();

错误发生在这里:

var day = new Date();
day = day.substring(4, 100);

JavaScript中的日期存储为具有许多功能的对象, substring不是其中之一。

您可以在日期对象上使用toString()方法,如下所示:

day.toString().substring(4, 100);

或者,您可以使用getDay()方法,该方法返回日期实例的工作日索引:

['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'][day.getDay()-1];

JSHint和JSLint仅识别语法错误。 由于以下原因,您收到的错误是运行时错误

day.substring(4, 100);
/* day is Date object which does not have any property/method called 'substring'
 * therefore day.substring will be undefined
 * and using undefined as a function like undefined(...) will give you error- undefined is not a function
 */

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM