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 ...
随机推荐
- keystone 认证深度研究分析
一.Keystone Token深度概述 Keystone作为OpenStack项目基础认证模块,目前支持的token类型分别是uuid.pkiz.pki.fernet. 首先,简要叙述一下这四种类型 ...
- python中 .write 无法向文件写入内容
问题代码如下 links = open("new") out = open("out.txt","w+") for link in link ...
- python opencv3 直线检测
git:https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 import numpy as np # 读入图像 ...
- JVM内存模型以及垃圾回收
JAVA堆的描述如下: 内存由Perm和Heap组成.其中Heap = {Old + NEW = { Eden , from, to } } JVM内存模型中分两大块: NEW Generation: ...
- ZOJ 1940 Dungeon Master 三维BFS
Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- phpunit Cannot redeclare class PHPUnit_Runner_Version
- vue各生命周期适合做的业务逻辑
一.实际项目中使用最多的Vue生命周期大概是 created mounted updated 二.各自适合做的业务逻辑 1. created 相当于是页面刚开始加载的状态,此时不能操作实例的 ...
- 使用Django来处理对于静态文件的请求
引言 本方法适用于linux+python2.7+django1.2,使用django自带的web服务. 同样也适用于sina app engine. 1.准备工作 准备css文件,准备js文件,准备 ...
- POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
- PIC JDM Prototype Programmer 1001
In need of a programmer for PIC micro controllers I decided to build my own one. This programmer has ...