示例工程 02-values-types-equality
另有 1 个文件未随站点发布(平台工程、二进制或超过 200KB)。
02-values-types-equality/01-typeof-table/main.js
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
// main.js —— typeof 全表
// 结论只用可运行的值来支撑:每一行都是 typeof 的真实返回。
class SampleClass {}
function sampleFn() {}
const generatorObject = (function* () {})();
export const SAMPLES = [
['undefined', undefined],
['null', null],
['true', true],
['42', 42],
['1n', 1n],
["'x'", 'x'],
['Symbol()', Symbol('s')],
['{}', {}],
['[]', []],
['function () {}', sampleFn],
['class SampleClass {}', SampleClass],
['/re/', /re/],
['new Date()', new Date()],
['Promise.resolve()', Promise.resolve()],
['[].values()(迭代器对象)', [].values()],
['(function*(){})()(生成器对象)', generatorObject],
['new Map()', new Map()],
['new Number(1)(装箱)', new Number(1)],
];
export function typeofTable() {
return SAMPLES.map(([label, value]) => ({ label, type: typeof value }));
}
export function run(log = console.log) {
const rows = typeofTable();
log('表达式 => typeof');
for (const row of rows) log(`${row.label} => ${row.type}`);
log('-'.repeat(46));
log("注意:typeof null === 'object',而 typeof [] === 'object',两者完全不同的东西。");
return rows;
}