示例工程 17-pre-module-globals
17-pre-module-globals/02-var-vs-let/main.test.mjs
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { probe, run } from './dist/main.js';
test('var 是全局属性,let 不是', () => {
globalThis.legacyVar = 'var';
const out = [];
const ret = run((msg) => out.push(msg));
assert.deepEqual(ret, ['legacyVar:global', 'modernLet:not-global']);
assert.deepEqual(out, ret);
assert.deepEqual(probe({}), ['legacyVar:not-global', 'modernLet:not-global']);
delete globalThis.legacyVar;
});