Created
May 10, 2016 13:30
-
-
Save burlistic/58abc95ffb00408c98c09d3a54dec04a to your computer and use it in GitHub Desktop.
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
"use strict"; | |
// Classes. A bit of sugar over prototype based OO pattern | |
class StringCalc { | |
// COnstructor.. just an excuse to use one really | |
constructor(decimalPrecision, calcName) { | |
this.decimalPrecision = decimalPrecision; | |
this.calcName = calcName; | |
this.sumTotal = 0; | |
} | |
add(stringInput) { | |
if (stringInput == "0" || stringInput == "") | |
{ | |
return 0; | |
} | |
// Let.. The new var. Mutable variable (simular to functional langs with mutable and unmutable variables) | |
let stringNumbers = stringInput.split(","); | |
this.sumTotal = 0; | |
// Arrow statement body (simular to c# lambda expresions) | |
stringNumbers.forEach(v => { | |
this.sumTotal = this.sumTotal + parseInt(v); | |
}); | |
return this.sumTotal; | |
} | |
} | |
class StringCalcDivision extends StringCalc { | |
constructor(decimalPrecision, calcName) { | |
// Super calls the base class | |
super(decimalPrecision, calcName); | |
this.newProperty = 'whatever'; | |
} | |
// Add a new method | |
divide(stringInput) { | |
if (stringInput == "0" || stringInput == "") { | |
return 0; | |
} | |
let stringNumbers = stringInput.split(","); | |
this.sumTotal = stringNumbers[0] / stringNumbers[1] | |
return this.sumTotal; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment