TypeScript简单的代码片段
TypeScript中,接口、接口实现、函数重载;
interface IThing{
name:string;
age:number;
sayHello:{
(name:string):string;
(age:number):number;
}
} class Thing implements IThing{
name:string;
age:number; sayHello(name:string):string;
sayHello(age:number):number; sayHello(arg:any):any{
if(typeof arg === 'string'){
return 'hello, ' + arg;
}else if(typeof arg === 'number'){
return arg*2;
}else
{
throw new Error('invalid input args!!!');
}
}
} interface ICar{
engine:string;
}
class Car implements ICar{
constructor(public engine:string){
}
}
var thing = new Thing();
thing.name = 'paul cheung';
thing.age = 23;
thing.sayHello("this is a string~~~");
var car = new Car("Tesxt");
document.body.innerText = car.engine;
对应的javascript代码:
var Thing = (function () {
function Thing() {
}
Thing.prototype.sayHello = function (arg) {
if (typeof arg === 'string') {
return 'hello, ' + arg;
} else if (typeof arg === 'number') {
return arg * 2;
} else {
throw new Error('invalid input args!!!');
}
};
return Thing;
})(); var Car = (function () {
function Car(engine) {
this.engine = engine;
}
return Car;
})();
var thing = new Thing();
thing.name = 'paul cheung';
thing.age = 23;
thing.sayHello("this is a string~~~");
var car = new Car("Tesxt");
document.body.innerText = car.engine;
TypeScript类定义和继承、module定义及使用:
class Auto{
engine:string;
constructor(engine:string){
this.engine = engine;
}
} class Truck extends Auto{
bigTires:bool;
constructor(engine:string,bigTires:bool){
super(engine);
this.bigTires = bigTires;
}
} module Shapes{
export class Person{
constructor(
public name:string,
public age:number
){}
}
}
var p = new Shapes.Person("paul cheung", 21) ///<reference path='shapes.ts'/>
javascript代码:
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var Auto = (function () {
function Auto(engine) {
this.engine = engine;
}
return Auto;
})(); var Truck = (function (_super) {
__extends(Truck, _super);
function Truck(engine, bigTires) {
_super.call(this, engine);
this.bigTires = bigTires;
}
return Truck;
})(Auto); var Shapes;
(function (Shapes) {
var Person = (function () {
function Person(name, age) {
this.name = name;
this.age = age;
}
return Person;
})();
Shapes.Person = Person;
})(Shapes || (Shapes = {}));
var p = new Shapes.Person("paul cheung", 21);
using http://www.typescriptlang.org/playground/ to do demo~~~
TypeScript简单的代码片段的更多相关文章
- js/jquery/html前端开发常用到代码片段
1.IE条件注释 条件注释简介 IE中的条件注释(Conditional comments)对IE的版本和IE非IE有优秀的区分能力,是WEB设计中常用的hack方法.条件注释只能用于IE5以上,IE ...
- 非常实用的PHP代码片段推荐
当使用PHP进行开发的时候,如果你自己收 藏 了一些非常有用的方法或者代码片段,那么将会给你的开发工作带来极大的便利.今天我们将介绍10个超级好用的PHP代码片段,希望大家能够喜欢! 1. 使用te ...
- Sublime Text3—Code Snippets(自定义代码片段)
摘要 程序员总是会不断的重复写一些简单的代码片段,为了提高编码效率,我们可以把经常用到的代码保存起来再调用. 平时用sublime安装各种插件,使用Tab键快速补全,便是snippets(可译为代码片 ...
- phpstorm设置代码片段
tab 向后推进 shift+tab 向前推进 ctrl+d 复制整行 ctrl+Y删除整行 代码片段就是代码快捷键,如果你设置了www.baidu.com这些内容,但是不想一直重复的打,可以设置个代 ...
- VS里的 代码片段(Code snippet)很有用,制作也很简单
工欲善其事必先利其器,而 Visual Studio 就是我们的开发利器. 上一篇文章,介绍了一个很棒的快捷键,如果你还没用过这个快捷键,看完之后应该会豁然开朗.如果你已经熟练的应用它,也会温故而知新 ...
- 经验分享:10个简单实用的 jQuery 代码片段
尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库.今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 您可能感兴趣的相 ...
- Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)
摘自:http://blog.csdn.net/shakespeare001/article/details/7926783 Adapter是连接后端数据和前端显示的适配器接口,是数据Data和UI( ...
- 10个简单实用的 jQuery 代码片段
尽管各种 JavaScirpt 框架和库层出不穷,jQuery 仍然是 Web 前端开发中最常用的工具库. 今天,向大家分享我觉得在网站开发中10个简单实用的 jQuery 代码片段. 1.平滑滚动到 ...
- [转]Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段
收藏ArrayAdapter.SimpleAdapter和BaseAdapter的一些简短代码片段,希望用时方便想起其用法. 1.ArrayAdapter 只可以简单的显示一行文本 代码片段: A ...
随机推荐
- Ubuntu的shell执行过程
登录shell(login shell)会执行.bash_profile,.bash_profile中会执行.profile,.profile中会执行.bashrc 非登录shell(non-logi ...
- Vue push() pop() shift() unshift() splice() sort() reverse() ...
Vue 变异方法 push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度. pop() 方法用于删除并返回数组的最后一个元素. shift() 方法用于把数组的第一个元素从其中删除,并返回 ...
- 51nod1085 背包问题【动态规划】
在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2--Wn(Wi为整数),与之相对应的价值为P1,P2--Pn(Pi为整数).求背包能够容纳的最大价值. Input 第1行,2个整数 ...
- Spring Cloud系列(三) 应用监控与管理Actuator
Spring Cloud系列(二) 应用监控与管理Actuator 前言:要想使用Spring Cloud ,Spring Boot 提供的spring-boot-starter-actuator模块 ...
- C# DataGridView 使用
之前咩有做个界面的东西,更没有使用过DataGirdView 这个控件. 现在本来是准备用DeV呢,结果发现我的DEV没有注册,只好暂时用这个DataGridView来替代使用了. 我现在要是设置两列 ...
- Shell入门基础
Shell的Helloworld #!/bin/bash echo "helloworld taosir" 执行方式 方式一:用 bash 或 sh 的相对或绝对路径(不用赋予脚本 ...
- Ubuntu 14.04远程登录服务器
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51285545 本文讲述在Ubuntu ...
- 怎么样调整FreeBSD时区问题
一般我们在安装系统的时候,都会遇到服务器时间不同步的情况.所以必须得设置为中国时区,比较简单的方法,就总结如下几点: 1.通过命令行启动图形界面更改 #sysinstall 请选择 configure ...
- hdu 1576扩展欧几里得算法
#include<stdio.h> #define ll long long /* 2.那么x,y的一组解就是x1*m1,y1*m1,但是由于满足方程的解无穷多个, 在实际的解题中一般都会 ...
- 洛谷—— P1097 统计数字
https://www.luogu.org/problem/show?pid=1097 题目描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*10^9).已知不相同的数 ...