原文: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. iOS Sprite Kit教程之真机测试以及场景的添加与展示

    iOS Sprite Kit教程之真机测试以及场景的添加与展示 IOS实现真机测试 在进行真机测试之前,首先需要确保设备已经连在了Mac(或者Mac虚拟机)上,在第1.9.1小节开始,设备就一直连接在 ...

  2. Python内存管理方式和垃圾回收算法解析

    在列表,元组,实例,类,字典和函数中存在循环引用问题.有 __del__ 方法的实例会以健全的方式被处理.给新类型添加GC支持是很容易的.支持GC的Python与常规的Python是二进制兼容的. 分 ...

  3. Linux网卡驱动

    <网络知识> a:网络模型               OSI模型               TCP模型 虽然OSI模型看着挺完美的,但是过于复杂,这样就会导致不实用,在Linux系统中 ...

  4. BZOJ 3963 HDU3842 [WF2011]MachineWorks cdq分治 斜率优化 dp

    http://acm.hdu.edu.cn/showproblem.php?pid=3842 写的check函数里写的<但是应该是<=,调了一下午,我是个zz. 就是普通的斜率优化因为有两 ...

  5. 【漏洞预警】方程式又一波大规模 0day 攻击泄漏,微软这次要血崩

    一大早起床是不是觉得阳光明媚岁月静好?然而网络空间刚刚诞生了一波核弹级爆炸!Shadow Brokers再次泄露出一份震惊世界的机密文档,其中包含了多个精美的 Windows 远程漏洞利用工具,可以覆 ...

  6. 【BZOJ-1194】潘多拉的盒子 拓扑排序 + DP

    1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 456  Solved: 215[Submit][Stat ...

  7. OpenGL ES 3.0 图元装配

    1. 前言 之前已经把纹理的渲染给弄出来了,但是又遇到一个新的问题,那就是图元装配,比如说我已经把图片给显示出来了,但是呢,并没有做到让它显示到具体的位置,而跟这个位置相关的则需要靠图元装配. 图元装 ...

  8. jmeter3.3—插件管理器的安装

    一.介绍JMeter Plugins 一直以来, JMeter Plugins 为我们提供了很多高价值的JMeter插件,比如: 用于服务器性能监视的 PerfMon Metrics Collecto ...

  9. ssh 远程链接时出现错误提示:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED

    ssh 远程链接时出现错误提示 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST I ...

  10. Python知识(1)----基础入门和进阶总结。

    今天把Python的语法过了一遍,学习了慕课网上的教程,简单易懂,1个小时就可以入门Python了.Python有两个主要的版本,Python2.7,Python3.5,后面的版本,改动较大,编写的程 ...