Created
May 31, 2014 21:11
-
-
Save codeBelt/65a82e76597f2fb6c2af to your computer and use it in GitHub Desktop.
TypeScript Classes and OOP examples (tsc TestApp.ts -out app.js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyNamespace { | |
export class Animal { | |
public furry:boolean; | |
public domestic:boolean; | |
constructor() { | |
console.log("new animal created"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyNamespace { | |
export class Brick { | |
public color:string = 'red'; | |
constructor() { | |
console.log('new '+ this.color +' brick created'); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///<reference path='Animal.ts'/> | |
module MyNamespace { | |
export class Cat extends Animal { | |
public family:string; | |
constructor() { | |
super(); | |
this.furry = true; | |
this.domestic = true; | |
this.family = "feline"; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>TypeScript is Awesome</title> | |
</head> | |
<body> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<script type="text/javascript" src="assets/scripts/app.js"></script> | |
<script> | |
$(document).ready(function(event) { | |
// Get class from namespace. | |
var TestApp = AnotherNamespace.TestApp; | |
var app = new TestApp(); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyNamespace { | |
export interface IWork { | |
work():void; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyNamespace { | |
export class Person { | |
private _age:number = 1; | |
constructor() { | |
} | |
public get age():number { | |
return this._age; | |
} | |
public set age(val:number) { | |
if(this._age > 0) { | |
this._age = val; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyNamespace { | |
export class Student implements IWork { | |
public averageGrade:number; | |
constructor() { | |
this.averageGrade = 4.07; | |
} | |
public work():void { | |
console.log("I am studying"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///<reference path='Teacher.ts'/> | |
///<reference path='Student.ts'/> | |
///<reference path='IWork.ts'/> | |
module MyNamespace { | |
export class Supervisor { | |
constructor(inst:IWork) { | |
inst.work(); | |
if(inst instanceof Teacher) { | |
var teacher:Teacher = <Teacher>inst; | |
console.log('Has tenure:', teacher.tenure); | |
} | |
if(inst instanceof Student) { | |
var student:Student = <Student>inst; | |
console.log('Average grade is:', student.averageGrade); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyNamespace { | |
export class Teacher implements IWork { | |
public tenure:boolean; | |
constructor() { | |
this.tenure = true; | |
} | |
public work():void { | |
console.log("I am teaching"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///<reference path='Brick.ts'/> | |
///<reference path='Wall.ts'/> | |
///<reference path='Cat.ts'/> | |
///<reference path='Teacher.ts'/> | |
///<reference path='Student.ts'/> | |
///<reference path='Supervisor.ts'/> | |
module AnotherNamespace { | |
// Take note, since the other classes have a different module name we need to import them. | |
// If the classes have the same module name then you don't need to import. | |
import Brick = MyNamespace.Brick; | |
import Wall = MyNamespace.Wall; | |
import Cat = MyNamespace.Cat; | |
import Teacher = MyNamespace.Teacher; | |
import Student = MyNamespace.Student; | |
import Supervisor = MyNamespace.Supervisor; | |
export class TestApp { | |
constructor() { | |
console.log("!!!!!!!!!!!! Class Example"); | |
var firstBrick:Brick = new Brick(); | |
console.log("!!!!!!!!!!!! Inheritance Example"); | |
var cat:Cat = new Cat(); | |
console.log("!!!!!!!!!!!! Composition Example"); | |
var myWall:Wall = new Wall(4,4); | |
console.log("!!!!!!!!!!!! Polymorphism Example"); | |
var supervisor1:Supervisor = new Supervisor(new Teacher()); | |
var supervisor2:Supervisor = new Supervisor(new Student()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///<reference path='Brick.ts'/> | |
module MyNamespace { | |
export class Wall { | |
public wallWidth:number; | |
public wallHeight:number; | |
constructor(w:number, h:number) { | |
this.wallWidth = w; | |
this.wallHeight = h; | |
this.build(); | |
} | |
public build():void { | |
for(var i:number = 0; i < this.wallHeight; i++) { | |
for(var j:number = 0; j < this.wallWidth; j++) { | |
var brick:Brick = new Brick(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
let phoneNumber;
if (Math.random() > 0.5) {
phoneNumber = "ronan";
} else {
phoneNumber = 7167762323;
}