js分号问题

111 阅读1分钟

解决方法:

在前面或者后面都可以加上分号

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // var a = 123
        // (function (){
        //     console.log('123456789')
        // })()
        //分号问题.html:11 Uncaught TypeError: 123 is not a function
        // at 分号问题.html:11



        var a = 123
        ;(function (){
            console.log('123456789')
        })()

        //123456789
        var b = '452122'
        [1,23,45].foreach(item => console.log(item))
        //分号问题.html:26 Uncaught TypeError: Cannot read property 'foreach' of undefined

        //错误理解
        //  var b = '452122'[45].foreach(item => console.log(item))  
        //'452122'[45]  undefined
    </script>
</body>
</html>