示例工程 14-runtime-syntax

14-runtime-syntax/02-namespace-output/src/main.ts

typescript · 0.5 KB

运行:14 · namespace-output

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

// 演示:namespace 生成 IIFE 与嵌套对象
export namespace Geometry {
  export const PI = 3.14159;
 
  export function area(radius: number): number {
    return PI * radius * radius;
  }
 
  export namespace Labels {
    export const circle = 'circle';
  }
}
 
export function run(log: (msg: string) => void = console.log): string[] {
  const lines = [
    Geometry.area(1).toFixed(2),
    Geometry.Labels.circle,
    String(Geometry.PI),
  ];
  for (const line of lines) {
    log(line);
  }
  return lines;
}