mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 07:33:57 +00:00
Fix formatting, close #990
Cleaned up mixed tabs/spaces Wrapped lines at 80 characters Fixed incorrect comment regarding property name
This commit is contained in:
parent
104ffc3608
commit
2ae3d41c27
@ -19,7 +19,7 @@ var isDone: boolean = false;
|
||||
var lines: number = 42;
|
||||
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;
|
||||
notSure = "maybe a string instead";
|
||||
notSure = false; // okay, definitely a boolean
|
||||
@ -33,20 +33,27 @@ var list: Array<number> = [1, 2, 3];
|
||||
enum Color {Red, Green, Blue};
|
||||
var c: Color = Color.Green;
|
||||
|
||||
//Lastly, "void" is used in the special case of a function not returning anything
|
||||
// Lastly, "void" is used in the special case of a function returning nothing
|
||||
function bigHorribleAlert(): void {
|
||||
alert("I'm a little annoying box!");
|
||||
}
|
||||
|
||||
//Functions are first class citizens, support the lambda "fat arrow" syntax and use type inference
|
||||
//All examples are equivalent, the same signature will be infered by the compiler, and same JavaScript will be emitted
|
||||
var f1 = function(i: number) : number { return i * i; }
|
||||
var f2 = function(i: number) { return i * i; } //Return type infered
|
||||
var f3 = (i : number) : number => { return i * i; }
|
||||
var f4 = (i: number) => { return i * i; } //Return type infered
|
||||
var f5 = (i: number) => i * i; //Return type infered, one-liner means no return keyword needed
|
||||
// Functions are first class citizens, support the lambda "fat arrow" syntax and
|
||||
// use type inference
|
||||
|
||||
//Interfaces are structural, anything that has the properties is compliant with the interface
|
||||
// The following are equivalent, the same signature will be infered by the
|
||||
// compiler, and same JavaScript will be emitted
|
||||
var f1 = function(i: number) : number { return i * i; }
|
||||
// Return type inferred
|
||||
var f2 = function(i: number) { return i * i; }
|
||||
var f3 = (i : number) : number => { return i * i; }
|
||||
// Return type inferred
|
||||
var f4 = (i: number) => { return i * i; }
|
||||
// Return type inferred, one-liner means no return keyword needed
|
||||
var f5 = (i: number) => i * i;
|
||||
|
||||
// Interfaces are structural, anything that has the properties is compliant with
|
||||
// the interface
|
||||
interface Person {
|
||||
name: string;
|
||||
// Optional properties, marked with a "?"
|
||||
@ -55,17 +62,19 @@ interface Person {
|
||||
move(): void;
|
||||
}
|
||||
|
||||
//..Object that implements the "Person" interface
|
||||
var p : Person = { name: "Bobby", move : () => {} }; //Can be treated as a Person since it has the name and age properties
|
||||
//..Objects that have the optional property:
|
||||
// Object that implements the "Person" interface
|
||||
// Can be treated as a Person since it has the name and move properties
|
||||
var p : Person = { name: "Bobby", move : () => {} };
|
||||
// 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
|
||||
// Is not a person because age is not a number
|
||||
var invalidPerson : Person = { name: "Bobby", age: true };
|
||||
|
||||
//..Interfaces can also describe a function type
|
||||
// Interfaces can also describe a function type
|
||||
interface SearchFunc {
|
||||
(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;
|
||||
mySearch = function(src: string, sub: string) {
|
||||
return src.search(sub) != -1;
|
||||
@ -76,10 +85,12 @@ class Point {
|
||||
// Properties
|
||||
x: number;
|
||||
|
||||
//Constructor - the public/private keywords in this context will generate the boiler plate code
|
||||
// for the property and the initialization in the constructor.
|
||||
// Constructor - the public/private keywords in this context will generate
|
||||
// the boiler plate code for the property and the initialization in the
|
||||
// constructor.
|
||||
// In this example, "y" will be defined just like "x" is, but with less code
|
||||
// Default values are also supported
|
||||
|
||||
constructor(x: number, public y: number = 0) {
|
||||
this.x = x;
|
||||
}
|
||||
@ -120,25 +131,25 @@ module Geometry {
|
||||
|
||||
var s1 = new Geometry.Square(5);
|
||||
|
||||
//..Local alias for referencing a module
|
||||
// Local alias for referencing a module
|
||||
import G = Geometry;
|
||||
|
||||
var s2 = new G.Square(10);
|
||||
|
||||
// Generics
|
||||
//..Classes
|
||||
// Classes
|
||||
class Tuple<T1, T2> {
|
||||
constructor(public item1: T1, public item2: T2) {
|
||||
}
|
||||
}
|
||||
|
||||
//..Interfaces
|
||||
// Interfaces
|
||||
interface Pair<T> {
|
||||
item1: T;
|
||||
item2: T;
|
||||
}
|
||||
|
||||
//..And functions
|
||||
// And functions
|
||||
var pairToTuple = function<T>(p: Pair<T>) {
|
||||
return new Tuple(p.item1, p.item2);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user