Created
January 30, 2017 17:28
-
-
Save lucnat/f8899d1470813f1f0fcf910513a5f81b to your computer and use it in GitHub Desktop.
Typescript OOP the right way
This file contains hidden or 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
// ----------------- VECTOR CLASS EXAMPLE ---------------- | |
class Vector { | |
components: [number]; | |
constructor(components: [number]){ | |
this.components = components; | |
} | |
// getters and setters | |
getDimension(): number { | |
// returns the vector's dimension | |
return this.components.length; | |
} | |
getComponents(): [number] { | |
// returns component array | |
return this.components; | |
} | |
get(i: number): number { | |
// returns i'th component | |
if(i >= this.components.length) | |
throw new Error('Vector index out of bounds'); | |
return this.components[i]; | |
} | |
plus(other: Vector): Vector { | |
// returns a new vector which is the sum of this and other. | |
if(this.getDimension() !== other.getDimension()) { | |
throw new Error('Dimensions do not match.'); | |
} | |
var a: [number]; | |
for(var i=0; i<this.getDimension(); i++){ | |
a[i] = this.get(i) + other.get(i); | |
} | |
return new Vector(a); | |
} | |
dot(other: Vector): number { | |
// returns the dot produvt between this and other | |
if(this.getDimension() !== other.getDimension()) { | |
throw new Error('Dimensions do not match. '); | |
} | |
var sum = 0; | |
for(var i=0; i<this.getDimension(); i++){ | |
sum += this.components[i] * other.components[i]; | |
} | |
return sum; | |
} | |
toString(): string { | |
var s = '('; | |
this.components.forEach(function(component){ | |
s += component + ','; | |
}); | |
s += ')'; | |
return s; | |
} | |
} | |
function zeroVector(n: number): Vector { | |
var a: [number]; | |
for(var i=0; i<n; i++){ | |
a[i] = 0; | |
} | |
console.log(a); | |
return new Vector(a); | |
} | |
var v1 = new Vector([1,2,3]); | |
var v2 = new Vector([-1,0,1]); | |
var dotProduct = v1.dot(v2); | |
var sum = v1.plus(v2); | |
console.log('Scalar Product test'); | |
console.log( v1.toString() + ' * ' + v2.toString() + ' = ' + dotProduct); | |
console.log('Plus test') | |
console.log( v1.toString() +' + '+ v2.toString() +' = '+ sum); | |
// ------------------ COMPLEX CLASS EXAMPLE ------------------ | |
class ComplexNumber{ | |
real: number; | |
imaginary: number; | |
constructor(real: number, imaginary: number){ | |
this.real = real; | |
this.imaginary = imaginary; | |
} | |
plus(other: ComplexNumber): ComplexNumber { | |
var sum: ComplexNumber = new ComplexNumber(this.real + other.real, this.imaginary+other.imaginary); | |
return sum; | |
} | |
toString(): string{ | |
return this.real + ' + ' + this.imaginary + 'i'; | |
} | |
} | |
var pi = new ComplexNumber(3.14159, 0); | |
var i = new ComplexNumber(0, 1); | |
var sum:ComplexNumber = pi.plus(i); | |
console.log(sum.toString()); | |
interface Person { | |
firstName: string; | |
lastName: string; | |
} | |
function greeter(person: Person): string { | |
return 'Hello, '+ person.firstName + ' '+ person.lastName; | |
} | |
var user: Person = { | |
firstName: 'Luca', | |
lastName: 'Naterop' | |
}; | |
console.log(greeter(user)); | |
// ------------------- INTEGRABLE INTERFACE EXAMPLE ------------------- | |
interface Integrable{ | |
eval(): number | |
} | |
class Polynomial implements Integrable{ | |
degree: number; | |
coefficients: [number]; | |
constructor(degree: number, coefficients: [number]){ | |
this.degree = degree; | |
this.coefficients = coefficients; | |
} | |
eval(): number { | |
} | |
toString(){ | |
var s = ''; | |
for(var i=0; i <= this.degree; i++){ | |
s += this.coefficients[i] + 'x^' + i + ' + '; | |
} | |
return s; | |
} | |
} | |
var myPoly = new Polynomial(2,[0,0,1]); | |
console.log(myPoly.toString()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment