<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        // 函数声明
        function foo(){
            // 函数体
            console.log("hello huchangxi")
        }
        // 函数调用
        foo() // hello huchangxi
        foo() // hello huchangxi
        // 函数参数
        function bar(x, y) {
            console.log(x, y)
        }
        bar(1, 2, 3) // 1 2
        bar(1) // 1 undefined
        bar(1,2) // 1 2
        // 默认参数
        function print_stu_info(name,gender="男"){
            console.log("姓名: "+name+" 性别: "+gender)
        }
        print_stu_info("huchangxi","男") // 姓名: huchangxi 性别: 男
        print_stu_info("hulanxi","女") // 姓名: hulanxi 性别: 女
        print_stu_info("xxx") // 姓名: xxx 性别: 男
        // arguments对象
        function add() {
            console.log(arguments) // 函数内置 Arguments(4) [1, 2, 3, 4, callee: ƒ, Symbol(Symbol.iterator): ƒ]
            var ret = 0
            for (var i in arguments) {
                ret += arguments[i]
            }
            return ret
        }
        var ret = add(1,2,3,4)
        console.log(ret); // 10 #相加
        //匿名函数
        (function (x, y) {
            console.log("匿名函数") // 匿名函数
        })(1, 2)
        // 函数作用域
        var x = 1 // 全局
        function test() {
            // var x = 10 // 局部
            x = 10 // 全局
            console.log(x)
        }
        test()
        console.log(x) // 10
    </script>
</head>
<body>
<h1>welcome to JS!</h1>
</body>
</html>
![]()