学习篇-TypeScript-命名空间

一、TypeScript-命名空间
1. 命名空间是什么
  • 使用namespace关键字将一段代码包裹起来,这段被包裹起来的逻辑代码就是一个命名空间。
2. 命名空间的好处
  • 在单一的空间中,如果存在两个变量或函数的名字完全相同,就会出现冲突。为了解决这类问题,只要将相同的变量和函数放在不通的空间中就可以完美解决此类烦恼。
  • 代码层面的归类和管理。将有相似功能的代码都归一到同一个空间下进行管理,方便其他代码引用,更多的是侧重代码的复用。
3. 命名空间的定义
  • 由关键字namespace + 命名空间名字,之后接一对花括号,里面放入具体需要声明和定义。

    namespace 命名空间的名字{
    	// 具体的声明
    }
    
    • 花括号中可以出现能够全局作用域出现的任意声明:类、变量、函数、接口以及其他的命名空间。
4. 命名空间的调用
  • 通过export 关键字将需要引用的类、变量、函数、接口以及其他的命名空间导出

    • 在同一个文件中,直接使用即可。如果是对象,属性这些直接使用“完全限定名”. 的方式访问,如果是接口直接实现即可。

    • 如果分离到多个文件时,则应使用三斜杠 /// 引用它,语法格式如下:

        	/// <reference path = "xxx.ts"/>
          // xxx.ts就是需要引用的命名空间的ts文件
      
5. 命名空间的示例
  • 定义

    namespace Computer {
      export const PI = 3.14;
      export function sum(first: number, second: number) {
        return first + second;
      }
    }
    
    // 在外部使用命名空间
    console.log(Computer.PI);
    console.log(Computer.sum(3, 4));
    
  • 拆分到多个文件中

    • computer1.ts

      namespace Computer {
        export const PI = 3.14;
      }
      
    • computer2.ts

      namespace Computer {
        export function sum(first: number, second: number) {
          return first + second;
        }
      }
      
    • 在index.ts中引用

      console.log(Computer.PI);
      console.log(Computer.sum(3, 4));
      
    • 如何使用?

      • 方式一

        • 使用tsc命令将其编译成js文件

        • 在index.html中,通过script标签引用

          <html lang="en">
            <head>
              <meta charset="UTF-8" />
              <title>Document</title>
              <!--引用-->
              <script src="./dist/computer1.js"></script>
              <script src="./dist/computer2.js"></script>
              <script src="./dist/index.js"></script>
            </head>
            <body>
            </body>
          </html>
          
        • 编译后的computer1.js

          "use strict";
          var Computer;
          (function (Computer) {
              Computer.PI = 3.14;
          })(Computer || (Computer = {}));
          
        • 编译后的computer2.js

          "use strict";
          var Computer;
          (function (Computer) {
              function sum(first, second) {
                  return first + second;
              }
              Computer.sum = sum;
          })(Computer || (Computer = {}));
          
        • 编译后的index.js

          "use strict";
          ///<reference path="computer1.ts"/>
          ///<reference path="computer2.ts"/>
          console.log(Computer.PI);
          console.log(Computer.sum(3, 4));
          
      • 方式二

        • 使用tsc --outfile 编译到的目标文件 需要编译的文件1 需要编译的文件2 需要编译的文件3 ...

          • 需要编译的文件,保持依赖关系

            tsc --outfile index.js computer1.ts computer2.ts index.ts
            
          • 在index.html中只需要引用

            <html lang="en">
              <head>
                <meta charset="UTF-8" />
                <title>Document</title>
                <!--引用-->
                <script src="./dist/index.js"></script>
              </head>
              <body>
              </body>
            </html>
            
          • 编译后的index.js

            var Computer;
            (function (Computer) {
                Computer.PI = 3.14;
            })(Computer || (Computer = {}));
            var Computer;
            (function (Computer) {
                function sum(first, second) {
                    return first + second;
                }
                Computer.sum = sum;
            })(Computer || (Computer = {}));
            console.log(Computer.PI);
            console.log(Computer.sum(3, 4));
            
      • 方式三

        • 通过///<reference path="…" 引入文件

          • 在index.ts中直接引用computer1.ts、computer2.ts文件

            ///<reference path="computer1.ts"/>
            ///<reference path="computer2.ts"/>
            console.log(Computer.PI);
            console.log(Computer.sum(3, 4));
            
          • 使用tsc命令

            tsc --outfile index.js index.ts 
            
          • 编译后的index.js

            var Computer;
            (function (Computer) {
                Computer.PI = 3.14;
            })(Computer || (Computer = {}));
            var Computer;
            (function (Computer) {
                function sum(first, second) {
                    return first + second;
                }
                Computer.sum = sum;
            })(Computer || (Computer = {}));
            ///<reference path="computer1.ts"/>
            ///<reference path="computer2.ts"/>
            console.log(Computer.PI);
            console.log(Computer.sum(3, 4));
            
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值