Skip to content

Instantly share code, notes, and snippets.

@codeBelt
Created May 31, 2014 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeBelt/65a82e76597f2fb6c2af to your computer and use it in GitHub Desktop.
Save codeBelt/65a82e76597f2fb6c2af to your computer and use it in GitHub Desktop.
TypeScript Classes and OOP examples (tsc TestApp.ts -out app.js)
module MyNamespace {
export class Animal {
public furry:boolean;
public domestic:boolean;
constructor() {
console.log("new animal created");
}
}
}
module MyNamespace {
export class Brick {
public color:string = 'red';
constructor() {
console.log('new '+ this.color +' brick created');
}
}
}
///<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";
}
}
}
<!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>
module MyNamespace {
export interface IWork {
work():void;
}
}
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;
}
}
}
}
module MyNamespace {
export class Student implements IWork {
public averageGrade:number;
constructor() {
this.averageGrade = 4.07;
}
public work():void {
console.log("I am studying");
}
}
}
///<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);
}
}
}
}
module MyNamespace {
export class Teacher implements IWork {
public tenure:boolean;
constructor() {
this.tenure = true;
}
public work():void {
console.log("I am teaching");
}
}
}
///<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());
}
}
}
///<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();
}
}
}
}
}
@saravey2021
Copy link

let phoneNumber;

if (Math.random() > 0.5) {
phoneNumber = "ronan";
} else {
phoneNumber = 7167762323;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment