示例工程 03-variables-scope-hoisting

03-variables-scope-hoisting/01-tdz-minimal/index.html

html · 0.7 KB

运行:03 · TDZ 最小复现

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>03 · TDZ 最小复现</title>
</head>
<body>
  <p>打开 Console 查看 TDZ 与 var 提升的差异。</p>
  <script>
    // classic 脚本的顶层:var 会创建 window 属性,let 不会。
    var classicVar = 'v';
    let classicLet = 'l';
    console.log("'classicVar' in window =>", 'classicVar' in window);
    console.log("'classicLet' in window =>", 'classicLet' in window);
    console.log('window.classicLet =>', window.classicLet);
    console.log('typeof classicLet(仍可访问,因为已初始化)=>', typeof classicLet);
  </script>
  <script type="module">
    import { run } from './main.js';
    run();
  </script>
</body>
</html>