Inner Classes with TypeScript
原文: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的更多相关文章
- angular2 学习笔记 (Typescript)
1.接口奇葩验证 interface Abc { name : string } function abc(obj : Abc) { } let ttc = { name: "adad&qu ...
- TypeScript 零基础入门
前言 2015 年末看过一篇文章<ES2015 & babel 实战:开发 npm 模块>,那时刚接触 ES6 不久,发觉新的 ES6 语法大大简化了 JavaScript 程序的 ...
- 玩转TypeScript(5)--环境声明
环境声明为TypeScript引入了一个作用域,但是对于产生的javaScript程序不会有任何影响.程序员可以使用环境声明来告之TypeScript,一些其他的组将将提供变量的声明.比如,默认情况下 ...
- [转]TypeScript Quick start
本文转自:http://www.typescriptlang.org/docs/tutorial.html Quick start Get started with a simple TypeScri ...
- TypeScript in 5 minutes
https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html Let’s get started by build ...
- [web建站] 极客WEB大前端专家级开发工程师培训视频教程
极客WEB大前端专家级开发工程师培训视频教程 教程下载地址: http://www.fu83.cn/thread-355-1-1.html 课程目录:1.走进前端工程师的世界HTML51.HTML5 ...
- 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- ...
- [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 ...
- TypeScript - Classes
简介 JavaScript语言基于函数和原型链继承机制的方式构建可重用的组件.这对于OO方面编程来说显得比较笨拙.在下一代的JavaScript标准ECMAScript 6为我们提供了基于class ...
随机推荐
- ThinkPHP3.2设置异常页面404跳转页面
在ThinkPHP3.2版本中当我们访问不存在的页面时会出现非常不友好错误提示页面,类如下图: 0 «上一篇:div非弹出框半透明遮罩实现全屏幕遮盖css实现»下一篇:利于反向代理绑定任意的域名 po ...
- 洛谷.2619.[国家集训队2]Tree I(带权二分 Kruskal)
题目链接 \(Description\) 给定一个无向带权连通图,每条边是黑色或白色.求一棵最小权的恰好有K条白边的生成树. \(Solution\) Kruskal是选取最小的n-1条边.而白边数有 ...
- Problem H: 深入浅出学算法009-韩信点兵
Description 秦朝末年,楚汉相争.有一次,韩信将1500名将士与楚王大将李锋交战.苦战一场,楚军不敌,败退回营,汉军也死伤四五百人,于是,韩信整顿兵马也返回大本营.当行至一山坡,忽有后军来报 ...
- Codeforces Round #359 (Div. 2) B. Little Robber Girl's Zoo 水题
B. Little Robber Girl's Zoo 题目连接: http://www.codeforces.com/contest/686/problem/B Description Little ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
- C#高级编程9-第4章 继承
继承是面向对象的一大特征.要深刻学习继承,需要学会使用调试的技巧来学习它,因为它比较抽象. 继承 继承是指一个具体的类型直接使用另一类型的某些数据成员或函数成员,继承的类是基类(父类),被继承的类是派 ...
- echarts3相关的各种定制化
在我刚把项目中的echarts从2.x版本升级到echarts3.x,折腾老久,终于交付了项目的时候,echarts4又出来了,先不管,还是把我echarts3.x遇到的和formatter相关的问题 ...
- ICD2 VPP limiter for new PIC microcontrollers.
http://www.circuitsathome.com/mcu/pic_vpp_limiter VOUT = 2.5V * ( 1 + 24/10 ) = 2.5 * 3.4 = 8.5V New ...
- GDB 自动化操作的技术-PYTHON
https://github.com/spacewander/debugger-utils http://python.jobbole.com/85415/ https://segmentfault. ...
- ansible saltstart puppet
百台以下用ansible , 百台-千台用saltstart , 千台以上用puppet