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 ...
随机推荐
- lamp 5.6.36 bug记录
后来发现另一个问题,php文字水印中文是乱码. 用yum安装lamp环境详见:https://blog.csdn.net/u010071211/article/details/80370201 在ce ...
- windows提权的几种姿势
想象这种画面:你拿到了一台机器上Meterpreter会话了,然后你准备运行 getsystem 命令进行提权,但如果提权没有成功,你就准备认输了吗?只有懦夫才会认输.但是你不是,对吗?你是一个勇者! ...
- 【BZOJ】4152: [AMPPZ2014]The Captain【SLF优化Spfa】
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2107 Solved: 820[Submi ...
- Nodejs线上日志部署
Nodejs 被越来越多的使用到线上系统中,但线上系统没有日志怎么行呢. 一.forever记录日志 我的线上系统使用forever来启动服务,最开始就直接使用了forever来记录 forever ...
- php发送get、post请求的6种方法代码示例
本文主要展示了php发送get.post请求的6种方法的代码示例,分别为使用file_get_contents .fopen.fsockopen.curl来发送GET和POST请求,代码如下: 方法1 ...
- Educational Codeforces Round 13 C. Joty and Chocolate 水题
C. Joty and Chocolate 题目连接: http://www.codeforces.com/contest/678/problem/C Description Little Joty ...
- Linux-JDK+Tomcat的安装笔记
Linux-JDK+Tomcat的安装 一.JDK的安装 1. 使用命令uname –a查看系统的版本确定系统的位数,然后去JDK官网下载相应位数的安装程序,进行安装. 2. 使用rz命令将下载的 ...
- CentOS 7 与老版本CentOS防火墙配置的区别
一.CentOS 7 以下版本防火墙的配置: 1.开放80,22,8080 端口/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT/sbin/ipt ...
- [Asp.net MVC]Html.AntiForgeryToken()
CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对网站 ...
- Property Finder – a Cross-Platform Xamarin MonoTouch Mobile App
Developers are now finding themselves having to author applications for a diverse range of mobile pl ...