A Program problem on the scope of js


        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </div>
    <script>
        function check(data) {
            if (data < 10) {
                return `0${data}`
            } else {
                return data;
            }
        }
        function dateInit() {
            var dateNow = new Date();

            function getYear() {
                var yearNow = dateNow.getFullYear();
                yearNow = check(yearNow);
                return yearNow;
            }

            function getMonth() {
                var monthNow = dateNow.getMonth();
                monthNow = check(monthNow);
                return monthNow;
            }

            function getDate() {
                var dayNow = dateNow.getDate();
                dayNow = check(dayNow);
                return dayNow;
            }

            function getHour() {
                var hourNow = dateNow.getHours();
                hourNow = check(hourNow);
                return hourNow;
            }

            function getMinus() {
                var minusNow = dateNow.getMinutes();
                minusNow = check(minusNow);
                return minusNow;
            }

            function getSeconds() {
                var SecondsNow = dateNow.getSeconds();
                SecondsNow = check(SecondsNow);
                return SecondsNow;
            }

            function getDay() {
                var dayArr = ["", "", "", "", "", "", ""];
                var dayNow = dateNow.getDay();
                return dayArr[dayNow];
            }
        }
        function writeDate() {
            dateInit();
            var spans = document.querySelectorAll("span");
            spans[0].innerHTML = getYear() + " ";
            spans[1].innerHTML = getMonth() + " ";
            spans[2].innerHTML = getDate() + " ";
            spans[3].innerHTML = getDay();
            spans[4].innerHTML = getHour() + "";
            spans[5].innerHTML = getMinus() + "";
            spans[6].innerHTML = getSeconds() + "";
            var timer = setInterval(function () {
                writeDate();
            }, 1000)
        }
        writeDate()
    </script>
    </body>

</html>

this is an automatically updated clock I wrote to allow browsers to report getYear is not defined . My understanding is that I have defined the getYear function globally and should be able to access it in writeDate (). I don"t know where there is an error in understanding, and how to change the code, which makes me a little confused now. Please point out, thank you very much.

Mar.29,2021

you don't have a global definition function. You define getYear () in dateInit () . Generally speaking, getYear () is a local function belonging to dateInit () , and dateInit () forms a closure. You cannot access getYear () inside dateInit () outside the function, but only in dateInit () . In contrast, your other getMonth () functions are the same, all undefined.


find a basic book on JS, such as JS Advanced programming, and take a good look at the chapter on closures.


Please take a good look at the code you have written-where is the global


there is no global, and the JS default variables are scoped at the function level.
and your function is defined inside the dateInit function, which can only be accessed within this function.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3eeea-2b7b7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3eeea-2b7b7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?