function-表达式(内含自执行函数)

156 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    // console.log(fn)
    // fn() // 为什么 变量提升

    // 函数直接量
    // function fn() { // 函数定义部分
    //   console.log(123)
    // }

    // console.log(fn)
    // 代码执行到这一行是 才将fn赋值为函数
    // 函数表达式 只能在函数定义后 使用
    // var fn = function (a) {
    //   console.log(a)
    //   // console.log(arguments)
    // }
    // console.log(fn)
    // fn(100)

    // 匿名函数 没有名字的函数

    // 自执行函数
    // (function(a) { // 声明形参
    //   console.log('自执行', a)
    //   console.log('自执行', arguments)
    // })(100) // 传实参

    // n 是用来接收 自执行函数执行后的返回值 并不是把这个函数赋值给变量n
    // var n = (function () {
    //   return 100
    // })()
    // console.log(n)

    // 自执行函数其他写法
    // +function() {

    // }()

    // ~function() {

    // }()

    // !function() {

    // }()
    
    var bar = 100;

    // 再前面加个分号
    ;(function() {

    })()

    // var b = 20
    // !function() {
    //   console.log(12312)
    // }()
  </script>
</body>
</html>

自执行函数的参考文档