ITeye博客

ionic UI(3)TypeScript - handbook

sillycat · 2016-03-22 23:21 · Mobile Platform · 移动开发 · Markdown

ionic UI(3)TypeScript - handbook 1 Basic Types boolean - var isDone :boolean = false; number - var height: number = 6; string - var name:string = ‘carl’; name = “sillycat”; Array - var list:number[] = [1,2,3]; var list:Array = [1,2,3]; Enum - enum Color { Red, Green, Blue }; var c: Color = Color.Green; usually, enum begin from 0, we can set the number as well. enum Color {Red = 1, Green = 2, Blue = 4}; var colorName: string = Color[2]; alert(colorName); The alert message is ‘Green' Any - var notSure: any = 4; notSure = “may be a string instead”; notSure = false; Void - function warnUser(): void { alert(“warning message."); } 2 Interfaces interface LabelledValue { label: string; } This interface requires there is a property label in the parameter object. Optional Properties in Interface interface SquareConfig { color?: string; width?: number; } Class Types interface ClockInterface { currentTime:Date; setTime(d:Date); } class Clock implements ClockInterface { currentTime: Date; setTime(d:Date) { this.currentTime = d; } constructor(h:number, m: number) { } } Extending Interfaces interface Shape { color: string; } interface Square extends Shape { sideLength: number; } var square = {}; square.color = “blue”; square.sideLength = 10; 3 Classes Inheritance of Class class Animal { name: string; contractor(theName:string) { this.name = theName; } move(meters: number = 0) { alert(this.name + “ can move “ + meters + “m.") } } class Snake extends Animal { constructor(name:string) { super(name); } move(meters = 5) { alert(“slithering…."); super.move(meters); } } Private/Public modifiers - public is always the default Getter / Setter var passcode = “secret passcode”; class Employee { private _fullName: string; get fullName(): string { return this._fullName; } set fullName(newName: string) { if(passcode && passcode == “secret passcode”){ this._fullName = newName; }else{ alert(“Unauthorized update of employee!"); } } } var employee = new Employee(); employee.fullName = “Carl Luo”; if(employee.fullName) { alert(employee.fullName); } 4 Modules >tsc —out sample.js Validation.ts LettersOnlyValidator.ts ZipCodeValidator.ts Test.ts 5 Functions default and optional parameters function buildName(firstName:string, lastName?:string){ } function buildName(firstName:string, lastName = “Smith”) { } 6 Generics More documents in the handbook, but I think I am ok that I only read the first few chapters. http://www.typescriptlang.org/Handbook References: http://www.typescriptlang.org/Handbook

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。