示例工程 23-esm-live-bindings

23-esm-live-bindings/02-top-level-this/src/main.ts

typescript · 0.3 KB

运行:23 · top-level-this

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

// 模块顶层的 this 是 undefined(ESM 规范)
export const topThis = this;
 
export function run(log: (msg: string) => void = console.log): string[] {
  const kind = topThis === undefined ? 'undefined' : typeof topThis;
  const lines = [`topThis:${kind}`];
  for (const line of lines) {
    log(line);
  }
  return lines;
}