Saturday, June 14, 2014

Type Script (Object Oriented Javascript)


Installation


  1. For Visual Studio 2012, use the link to download. Download and Install the Typescript plugin for Visual Studio
  2. For Visual Studio 2013, Download Visual Studio Update 2

Configuration

enter image description here
enter image description here


Code Example


You can test the examples at TypeScript Playground.

1- Method parameters: following example show how to define the typed parameters to the function. in the following line, it shows that meters is of type number.

 move(meters: number) {}



2- Variable type: Following example informs how to define variables of type string.

greeting: string;



3- Defining Class: Following code shows how to define the class,

class Animal {
}


4- Constructor: Following code shows how to define constructor

class Animal {
    constructor(public name: string) { }
}

5- Inheritance: Following code shows how to define inheritance,
class Horse extends Animal {
}

6- Calling Base Class Method: Following code shows how to call base class method,
super.move(5);


Complete Code

class Animal {
    constructor(public name: string) { }
    move(meters: number) {
        alert(this.name + " moved " + meters + "m.");
    }
}

class Snake extends Animal {
    constructor(name: string) { super(name); }
    move() {
        alert("Slithering...");
        super.move(5);
    }
}

class Horse extends Animal {
    constructor(name: string) { super(name); }
    move() {
        alert("Galloping...");
        super.move(45);
    }
}

var sam = new Snake("Sammy the Python");
var tom: Animal = new Horse("Tommy the Palomino");

sam.move();
tom.move(34);

Book

Please find the Complete TypeScript Book at 

Video



No comments:

Post a Comment