示例工程 17-pre-module-globals

17-pre-module-globals/02-var-vs-let/src/main.ts

typescript · 0.5 KB

运行:17 · var-vs-let

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

// 演示:var 成为全局对象属性,let 只存在于脚本的词法作用域
export function probe(scope: object): string[] {
  return [
    `legacyVar:${'legacyVar' in scope ? 'global' : 'not-global'}`,
    `modernLet:${'modernLet' in scope ? 'global' : 'not-global'}`,
  ];
}
 
export function run(log: (msg: string) => void = console.log): string[] {
  const lines = probe(globalThis);
  for (const line of lines) {
    log(line);
  }
  return lines;
}