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 ...
随机推荐
- MongoDB复制原理
##mongodb复制(主从服务器数据备份, 一个主服务器可以有很多个从服务器) #mongodb的复制至少需要两个节点.其中一个是主节点,负责处理客户端请求,其余的都是从节点,负责复制主节点上的数据 ...
- Redis在Window服务下的安装
Redis 安装 1.首先在Windows下下载安装Redis 下载地址:https://github.com/MicrosoftArchive/redis/releases 根据你电脑系统的实际情况 ...
- 【UOJ #221】【NOI 2016】循环之美
http://uoj.ac/problem/221 因为\(a\)和\(b\)不互质时,\(\frac ab=\frac{\frac a{(a,b)}}{\frac b{(a,b)}}\),所以只用求 ...
- CSS实现背景透明,文字不透明
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- CentOS7.0安装Nginx-1.12.0
一.安装准备 首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++.gcc.openssl-devel.pcre-devel和zlib ...
- BZOJ 1854: [Scoi2010]游戏 并查集
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 2672 Solved: 958[Submit][Status][ ...
- 机器学习(1):Logistic回归原理及其实现
Logistic回归是机器学习中非常经典的一个方法,主要用于解决二分类问题,它是多分类问题softmax的基础,而softmax在深度学习中的网络后端做为常用的分类器,接下来我们将从原理和实现来阐述该 ...
- Lingoes 一款功能强大、简明易用的多语言词典和文本翻译软件
Lingoes 软件自述 Lingoes 是一款功能强大.简明易用的多语言词典和文本翻译软件,支持多达80种语言互查互译,这些语言包括 英.法.德.意.俄.中.日.韩.西.葡.阿拉伯语 及更多... ...
- linux内核 asmlinkage宏
http://blog.chinaunix.net/uid-7390305-id-2057287.html
- 使用 C++ 的 StringBuilder 提升 4350% 的性能
http://blog.jobbole.com/109663/?utm_source=blog.jobbole.com&utm_medium=relatedPosts http://msdn. ...