个人自学前端46-function()和new function()的区别

<script>
export default {
  created() {
    this.test1()
    this.test2()
    this.test3()
  },
  methods: {
    test1() {
      // 基础用法
      const a = 'a1'
      function fun() {
        console.log(a)
      }
      fun()
    },
    test2() {
      // 同test1
      const a = 'a2'
      const fun = function() {
        console.log(a)
      }
      fun()
    },
    test3() {
      // 加new 会立刻执行
      const a = 'a3'
      // eslint-disable-next-line no-unused-vars
      const fun = new function() {
        console.log(a)
      }
    }
  }
}
</script>
posted @ 2023-03-01 09:40  暗鸦08  阅读(19)  评论(0)    收藏  举报