mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Added section on Modules
Indented comments and edited the Interfaces part. Still missing indexers and Generics.
This commit is contained in:
parent
b5ccc1d83c
commit
0d022b14c0
@ -18,7 +18,7 @@ var isDone: boolean = false;
|
|||||||
var lines: number = 42;
|
var lines: number = 42;
|
||||||
var name: string = "Anders";
|
var name: string = "Anders";
|
||||||
|
|
||||||
//When it's impossible to know, there is the "Any" type
|
//..When it's impossible to know, there is the "Any" type
|
||||||
var notSure: any = 4;
|
var notSure: any = 4;
|
||||||
notSure = "maybe a string instead";
|
notSure = "maybe a string instead";
|
||||||
notSure = false; // okay, definitely a boolean
|
notSure = false; // okay, definitely a boolean
|
||||||
@ -50,40 +50,32 @@ interface Person {
|
|||||||
name: string;
|
name: string;
|
||||||
//Optional properties, marked with a "?"
|
//Optional properties, marked with a "?"
|
||||||
age?: number;
|
age?: number;
|
||||||
|
//And of course functions
|
||||||
|
move(): void;
|
||||||
}
|
}
|
||||||
//Object that implements the "Person" interface
|
|
||||||
var p : Person = { name: "Bobby" }; //Can be treated as a Person since it has the name and age properties
|
//..Object that implements the "Person" interface
|
||||||
//Objects that have the optional property:
|
var p : Person = { name: "Bobby", move : () => {} }; //Can be treated as a Person since it has the name and age properties
|
||||||
var validPerson : Person = { name: "Bobby", age: 42 };
|
//..Objects that have the optional property:
|
||||||
|
var validPerson : Person = { name: "Bobby", age: 42, move: () => {} };
|
||||||
var invalidPerson : Person = { name: "Bobby", age: true }; //Is not a person because age is not a number
|
var invalidPerson : Person = { name: "Bobby", age: true }; //Is not a person because age is not a number
|
||||||
|
|
||||||
//Interfaces can also define method signatures:
|
//..Interfaces can also describe a function type
|
||||||
interface PersonWhoCanTalk {
|
|
||||||
sayHello(otherPersonsName: string): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
//And also indexers, both with number and string
|
|
||||||
interface PersonWhoCanBeIndexed {
|
|
||||||
[index: number]: string;
|
|
||||||
}
|
|
||||||
//TODO
|
|
||||||
|
|
||||||
//Interfaces can also describe a function type
|
|
||||||
interface SearchFunc {
|
interface SearchFunc {
|
||||||
(source: string, subString: string): boolean;
|
(source: string, subString: string): boolean;
|
||||||
}
|
}
|
||||||
//Only the parameters' types are important, names are not important.
|
//..Only the parameters' types are important, names are not important.
|
||||||
var mySearch: SearchFunc;
|
var mySearch: SearchFunc;
|
||||||
mySearch = function(src: string, sub: string) {
|
mySearch = function(src: string, sub: string) {
|
||||||
return src.search(sub) != -1;
|
return src.search(sub) != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Classes
|
//Classes - members are public by default
|
||||||
class Point {
|
class Point {
|
||||||
//Properties
|
//Properties
|
||||||
x: number;
|
x: number;
|
||||||
|
|
||||||
//Constructor - the public/private keywords are shortcuts to generate the code for a property and its initialization
|
//Constructor - the public/private keywords in this context are shortcuts to generate the code for a property
|
||||||
//Equivalent to "x" in this case
|
//Equivalent to "x" in this case
|
||||||
//Default values are also supported
|
//Default values are also supported
|
||||||
constructor(x: number, public y: number = 0) {
|
constructor(x: number, public y: number = 0) {
|
||||||
@ -113,7 +105,23 @@ class Point3D extends Point {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Modules
|
//Modules, "." can be used as separators for sub modules
|
||||||
|
module Geometry {
|
||||||
|
export class Square {
|
||||||
|
constructor(public sideLength: number = 0) {
|
||||||
|
}
|
||||||
|
area() {
|
||||||
|
return Math.pow(this.sideLength, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var s1 = new Geometry.Square(5);
|
||||||
|
|
||||||
|
//..Local alias for rreferencing a module
|
||||||
|
import G = Geometry;
|
||||||
|
|
||||||
|
var s2 = new G.Square(10);
|
||||||
|
|
||||||
//Generics
|
//Generics
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user