原文:https://blog.oio.de/2014/03/21/inner-classes-typescript/

b.ts

class Foo {
sex:string;
say(){
new Foo.InnerFoo('aaa').doIt();
}
} module Foo {
export class InnerFoo {
constructor(name:string){
console.log(name);
}
doIt() {
console.log('inner class do it.');
}
}
} let a = new Foo.InnerFoo('fly');
let b = new Foo();
b.say();

  

-----------------------------------------------------------------

In TypeScript, there is no exlicit concept like inner classes.
So what you cannot do in TypeScript is as follows:

1
2
3
4
5
6
class Foo {
  export class InnerFoo {
  }
}
 
new Foo.InnerFoo();

You can achieve something similar by merging a class with a module containing the inner class.

1
2
3
4
5
6
7
8
9
10
class Foo {
}
 
module Foo {
    export class InnerFoo {
      doIt(){}
    }
}
 
new Foo.InnerFoo();

The merging happens implicitely by using the same name for the class and the module.

Of course you can use the inner class in the outer class:

1
2
3
4
5
6
7
8
9
10
11
class Foo {
    doSomethingWithInnerFoo() {
        new Foo.InnerFoo().doIt();
    }
}
 
module Foo {
    export class InnerFoo { doIt(){} }
}
 
new Foo().doSomethingWithInnerFoo();

The only downside here is that the inner class must be exported, so it is always visible to the outside – there are no private inner classes.

Inner Classes with TypeScript的更多相关文章

  1. angular2 学习笔记 (Typescript)

    1.接口奇葩验证 interface Abc { name : string } function abc(obj : Abc) { } let ttc = { name: "adad&qu ...

  2. TypeScript 零基础入门

    前言 2015 年末看过一篇文章<ES2015 & babel 实战:开发 npm 模块>,那时刚接触 ES6 不久,发觉新的 ES6 语法大大简化了 JavaScript 程序的 ...

  3. 玩转TypeScript(5)--环境声明

    环境声明为TypeScript引入了一个作用域,但是对于产生的javaScript程序不会有任何影响.程序员可以使用环境声明来告之TypeScript,一些其他的组将将提供变量的声明.比如,默认情况下 ...

  4. [转]TypeScript Quick start

    本文转自:http://www.typescriptlang.org/docs/tutorial.html Quick start Get started with a simple TypeScri ...

  5. TypeScript in 5 minutes

    https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html Let’s get started by build ...

  6. [web建站] 极客WEB大前端专家级开发工程师培训视频教程

    极客WEB大前端专家级开发工程师培训视频教程  教程下载地址: http://www.fu83.cn/thread-355-1-1.html 课程目录:1.走进前端工程师的世界HTML51.HTML5 ...

  7. Why does Typescript use the keyword “export” to make classes and interfaces public?

    原文: https://stackoverflow.com/questions/15760462/why-does-typescript-use-the-keyword-export-to-make- ...

  8. [TypeScript] Create a fluent API using TypeScript classes

    You can create an easy to chain API using TypeScript classes. Learn about the thisreturn type annota ...

  9. TypeScript - Classes

    简介 JavaScript语言基于函数和原型链继承机制的方式构建可重用的组件.这对于OO方面编程来说显得比较笨拙.在下一代的JavaScript标准ECMAScript 6为我们提供了基于class ...

随机推荐

  1. Vue-router浅析(二)

    一.导航守卫 经常在项目中,会对页面做一些权限控制,所以需要对路由变化做出处理,跳转或取消相应的路由. 导航:即路由发生变化.导航守卫即对路由变化做出处理.当然也分几种情况:对全局变化做处理.对&qu ...

  2. php的curl也没这么复杂

    许多同学在第一次使用curl的时候感觉一个头两个大(包括我在内),看着这一条条的curl_setopt函数完全摸不着头脑,不过在你花10分钟看了我的介绍后相信你以后也能轻松戏耍php的curl了 首先 ...

  3. python开发_tkinter_多级子菜单

    在之前的blog中有提到python的tkinter中的菜单操作 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 python开发_tkinter_窗口控件_自 ...

  4. wikioi 1080 线段树练习 树状数组

    1080 线段树练习 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond       题目描述 Description 一行N个方格,开始每个格子里都有一个整数.现 ...

  5. hihocoder #1015 KMP

    #include<stdio.h> #include<iostream> #include<math.h> #include<string.h> usi ...

  6. 体感设备:因特尔 Intel RealSense R200,乐视LeTV Pro Xtion和Orb奥比中光bec Astra比较

    最近调试三个个厂家的体感设备,第一个是Intel的RealSense R200(参数规格:分辨率:1080p,深度有效距离:0.51-4,USB3.0),第二个是乐视LeTV Pro Xtion(参数 ...

  7. 客户端程序获取自己的ip、isp、地理位置等信息

    @ 比如说你需要收集用户信息,又或者要通过这些信息让用户登陆合适的服务器(北京联通用户登陆北京联通服务器). @ 淘宝和新浪都提供了类似的API,你只需要发送一个http请求,它就返回一个json格式 ...

  8. python模块整理30-uui模块

    http://www.cnblogs.com/dkblog/archive/2011/10/10/2205200.htmlhttp://blog.csdn.net/zhaoweikid/article ...

  9. oracle备份恢复学习

    备份和恢复是常遇到的操作,逻辑备份和物理备份.物理备份又分为冷备份和热备份. 一.逻辑备份,恢复 是指使用exp命令,简单易行,不影响正常的数据库操作.exp   用户名/密码   file=/hom ...

  10. HDU 1890 Robotic Sort (splay tree)

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...