原文: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. 使用IIS实现反向代理

    IIS的反向代理是通过ARR模块来完成的,ARR模块需要另外安装,而且只能通过Web PlatForm Installer安装.关于安装来源与步骤,帖子已有很多,不做描述.启用“Application ...

  2. JavaSE基础之double数据类型的格式化

    JavaSE基础之double数据类型的格式化 1.double 数据类型的格式化工具类:DoubleFormatUtil.java package cn.com.zfc.util; import j ...

  3. POJ1222 EXTENDED LIGHTS OUT 高斯消元 XOR方程组

    http://poj.org/problem?id=1222 在学校oj用搜索写了一次,这次写高斯消元,haoi现场裸xor方程消元没写出来,真实zz. #include<iostream> ...

  4. 216. 组合总和 III

    216. 组合总和 III 题意 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的 ...

  5. ROS知识(10)----smach_viewer的Graph view不能显示状态图

    1.问题 在运行ROS by Example 2--Indigo版本中,运行 smach_viewer,再运行巡逻,命令如下: $ rosrun smach_viewer smach_viewer.p ...

  6. HAproxy和TIME WAIT的一次问题排查

    近日平稳运行了将近4年的发号器突然出现问题,在元旦0分的时候出现短暂的性能下降,导致发号失败率飙高到一个不可接收的值,哎,意外总是发生在你想不到的地方. 这几天赶紧和小伙伴们赶紧追查原因,制定改造方案 ...

  7. 回顾下$.ajax()方法参数

    1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...

  8. 工作记录(1)- js问题

    也是好久不写博客了,确实懒了:想想应该把node.js的东西写完整比较好,在抽时间吧: 这几天在做阿里巴巴的一个页面展示,里面设计到了一些js的问题,中途也遇到了一些幼稚的问题, 算是简单记录一下,以 ...

  9. TLC2262和TLC2264 轨对轨运算放大器

    TLC2262和TLC2264分别是TI公司双路和四路运算放大器,两种器件可以在单电源或双电源条件下供电,从而增强了动态的范围,可以达到轨对轨输出的性能.TLC226X系列与TLC225X的微功耗和T ...

  10. 修改WampServer的默认端口

    WampServer默认的安装端口是80,容易和已安装的ISS等其他服务冲突,导致WampServer无法启动. 无法启动的现象如下: 1.apache服务无法启动.问题所在:80端口冲突. 2.在浏 ...