JS eval() 动态执行函数

527 阅读1分钟
<script>
  // 动态执行计算公式
  try {
    const formula  = '(1)/(1))'
    eval(formula)
  } catch (error) {
    console.log(error);
  }

  // 动态监测函数是否存在
  const funcType = typeof(eval('func'))
  console.log(funcType) // function

  // 动态执行函数
  eval('func()')
  function func() {
    console.log('动态执行函数')
  }
</script>