Last active
December 10, 2015 18:38
-
-
Save Cacodaimon/4476016 to your computer and use it in GitHub Desktop.
Quick comparison between TypeScript and JavaScript, with a semantic error (sub method missing in Vector3 class).
Used @ cacodaemon.de
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
C:\Users\Caco\..>tsc TestTs.ts | |
C:/Users/Caco/../TestTs.ts(62,26) | |
: Cannot convert 'Vector2' to 'Vector3' |
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
function Vector2(x, y) { | |
this.x = x ? x : 1; | |
this.y = y ? y : 1; | |
this.add = function(vector2) { | |
return new Vector2(this.x + vector2.x, this.y + vector2.y); | |
}; | |
this.sub = function(vector2) { | |
return new Vector2(this.x - vector2.x, this.y - vector2.y); | |
}; | |
this.dot = function(vector2) { | |
return this.x * vector2.x + this.y * vector2.y | |
}; | |
this.magnitude = function() { | |
return Math.sqrt(this.x * this.x + this.y * this.y); | |
}; | |
this.toString = function() { | |
return 'X: ' + this.x + ' Y: ' + this.y; | |
}; | |
}; | |
var vector2A = new Vector2(3, 3); | |
var vector2B = new Vector2(6, 6); | |
var vector2C = vector2A.add(vector2B); | |
var vector2D = vector2A.sub(vector2B); | |
console.log('Vector2 Add: ' + vector2A.add(vector2B)); | |
console.log('Vector2 Sub: ' + vector2A.sub(vector2B)); | |
console.log('Vector2 Dot: ' + vector2A.dot(vector2B)); | |
console.log('Vector2 Magnitdue A: ' + vector2A.magnitude()); | |
console.log('Vector2 Magnitdue B: ' + vector2B.magnitude()); | |
function Vector3(x, y, z) { | |
this.x = x ? x : 1; | |
this.y = y ? y : 1; | |
this.z = z ? z : 1; | |
this.add = function(vector3) { | |
return new Vector3(this.x + vector3.x, this.y + vector3.y, this.z + vector3.z); | |
}; | |
this.dot = function(vector2) { | |
return this.x * vector2.x + this.y * vector2.y + this.z * vector2.z; | |
}; | |
this.magnitude = function() { | |
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); | |
}; | |
this.toString = function() { | |
return 'X: ' + this.x + ' Y: ' + this.y + ' Z: ' + this.z; | |
}; | |
}; | |
Vector3.prototype = new Vector2(); | |
var vector3A = new Vector3(3, 3, 3); | |
var vector3B = new Vector3(6, 6, 6); | |
var vector3C = vector3A.add(vector3B); | |
var vector3D = vector3A.sub(vector3B); | |
console.log('Vector3 Add: ' + vector3A.add(vector3B)); | |
console.log('Vector3 Sub: ' + vector3A.sub(vector3B)); | |
console.log('Vector3 Dot: ' + vector3A.dot(vector3B)); | |
console.log('Vector3 Magnitdue A: ' + vector3A.magnitude()); | |
console.log('Vector3 Magnitdue B: ' + vector3B.magnitude()); |
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
var __extends = this.__extends || function (d, b) { | |
function __() { this.constructor = d; } | |
__.prototype = b.prototype; | |
d.prototype = new __(); | |
}; | |
var Vector2 = (function () { | |
function Vector2(x, y) { | |
if (typeof x === "undefined") { x = 1; } | |
if (typeof y === "undefined") { y = 1; } | |
this.x = x; | |
this.y = y; | |
} | |
Vector2.prototype.add = function (vector2) { | |
return new Vector2(this.x + vector2.x, this.y + vector2.y); | |
}; | |
Vector2.prototype.sub = function (vector2) { | |
return new Vector2(this.x - vector2.x, this.y - vector2.y); | |
}; | |
Vector2.prototype.dot = function (vector2) { | |
return this.x * vector2.x + this.y * vector2.y; | |
}; | |
Vector2.prototype.magnitude = function () { | |
return Math.sqrt(this.x * this.x + this.y * this.y); | |
}; | |
Vector2.prototype.toString = function () { | |
return 'X: ' + this.x + ' Y: ' + this.y; | |
}; | |
return Vector2; | |
})(); | |
; ; | |
var vector2A = new Vector2(3, 3); | |
var vector2B = new Vector2(6, 6); | |
var vector2ADD = vector2A.add(vector2B); | |
var vector2SUB = vector2A.sub(vector2B); | |
var vector2DOT = vector2A.dot(vector2B); | |
console.log('Vector2 Add: ' + vector2ADD); | |
console.log('Vector2 Sub: ' + vector2SUB); | |
console.log('Vector2 Dot: ' + vector2DOT); | |
console.log('Vector2 Magnitdue A: ' + vector2A.magnitude()); | |
console.log('Vector2 Magnitdue B: ' + vector2B.magnitude()); | |
var Vector3 = (function (_super) { | |
__extends(Vector3, _super); | |
function Vector3(x, y, z) { | |
if (typeof x === "undefined") { x = 1; } | |
if (typeof y === "undefined") { y = 1; } | |
if (typeof z === "undefined") { z = 1; } | |
_super.call(this, x, y); | |
this.z = z; | |
} | |
Vector3.prototype.add = function (vector3) { | |
return new Vector3(this.x + vector3.x, this.y + vector3.y, this.z + vector3.z); | |
}; | |
Vector3.prototype.dot = function (vector3) { | |
return this.x * vector3.x + this.y * vector3.y + this.y * vector3.y; | |
}; | |
Vector3.prototype.magnitude = function () { | |
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); | |
}; | |
Vector3.prototype.toString = function () { | |
return _super.prototype.toString.call(this) + ' Z: ' + this.z; | |
}; | |
return Vector3; | |
})(Vector2); | |
var vector3A = new Vector3(3, 3, 3); | |
var vector3B = new Vector3(6, 6, 6); | |
var vector3ADD = vector3A.add(vector3B); | |
var vector3SUB = vector3A.sub(vector3B); | |
var vector3DOT = vector3A.dot(vector3B); | |
console.log('Vector3 Add: ' + vector3ADD); | |
console.log('Vector3 Sub: ' + vector3SUB); | |
console.log('Vector3 Dot: ' + vector3DOT); | |
console.log('Vector3 Magnitdue A: ' + vector3A.magnitude()); | |
console.log('Vector3 Magnitdue B: ' + vector3B.magnitude()); |
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
class Vector2 { | |
constructor (public x: number = 1, public y: number = 1) { } | |
public add (vector2: Vector2) : Vector2 { | |
return new Vector2(this.x + vector2.x, this.y + vector2.y); | |
} | |
public sub (vector2: Vector2) : Vector2 { | |
return new Vector2(this.x - vector2.x, this.y - vector2.y); | |
} | |
public dot (vector2: Vector2) : number { | |
return this.x * vector2.x + this.y * vector2.y; | |
} | |
public magnitude () : number { | |
return Math.sqrt(this.x * this.x + this.y * this.y); | |
} | |
public toString() : string { | |
return 'X: ' + this.x + ' Y: ' + this.y; | |
} | |
}; | |
var vector2A: Vector2 = new Vector2(3, 3); | |
var vector2B: Vector2 = new Vector2(6, 6); | |
var vector2ADD: Vector2 = vector2A.add(vector2B); | |
var vector2SUB: Vector2 = vector2A.sub(vector2B); | |
var vector2DOT: number = vector2A.dot(vector2B); | |
console.log('Vector2 Add: ' + vector2ADD); | |
console.log('Vector2 Sub: ' + vector2SUB); | |
console.log('Vector2 Dot: ' + vector2DOT); | |
console.log('Vector2 Magnitdue A: ' + vector2A.magnitude()); | |
console.log('Vector2 Magnitdue B: ' + vector2B.magnitude()); | |
class Vector3 extends Vector2 { | |
constructor (x: number = 1, y: number = 1, public z: number = 1) { | |
super(x, y); | |
} | |
public add (vector3: Vector3) : Vector3 { | |
return new Vector3(this.x + vector3.x, this.y + vector3.y, this.z + vector3.z); | |
} | |
public dot (vector3: Vector3) : number { | |
return this.x * vector3.x + this.y * vector3.y + this.z * vector3.z; | |
} | |
public magnitude () : number { | |
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); | |
} | |
public toString () : string { | |
return super.toString() + ' Z: ' + this.z; | |
} | |
} | |
var vector3A: Vector3 = new Vector3(3, 3, 3); | |
var vector3B: Vector3 = new Vector3(6, 6, 6); | |
var vector3ADD: Vector3 = vector3A.add(vector3B); | |
var vector3SUB: Vector3 = vector3A.sub(vector3B); | |
var vector3DOT: number = vector3A.dot(vector3B); | |
console.log('Vector3 Add: ' + vector3ADD); | |
console.log('Vector3 Sub: ' + vector3SUB); | |
console.log('Vector3 Dot: ' + vector3DOT); | |
console.log('Vector3 Magnitdue A: ' + vector3A.magnitude()); | |
console.log('Vector3 Magnitdue B: ' + vector3B.magnitude()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment