Created
March 24, 2016 18:03
-
-
Save maciejlew/ce9d59ad88ec7cbc9449 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
function Dose() { | |
var _min = null; | |
var _max = null; | |
var checkType = function(value) { | |
if (!(value instanceof Number) && !(typeof value === 'number')) { | |
throw new TypeError('Dose value must be numeric!'); | |
} | |
}; | |
var checkRange = function(value) { | |
if (value < 0) { | |
throw new RangeError('Dose value can not be negative!'); | |
} | |
} | |
this.setMin = function(min) { | |
checkType(min); | |
checkRange(min); | |
if (min instanceof Number) { | |
_min = min; | |
} else if (typeof min === 'number') { | |
_min = new Number(min); | |
} | |
}; | |
this.setMax = function(max) { | |
checkType(max); | |
checkRange(max); | |
if (max instanceof Number) { | |
_max = max; | |
} else if (typeof max === 'number') { | |
_max = new Number(max); | |
} | |
}; | |
this.getMin = function() { | |
return _min; | |
}; | |
this.getMax = function() { | |
return _max; | |
}; | |
} | |
Dose.prototype = new Dose(); | |
var drug_dose = new Dose(); | |
drug_dose.setMin(1); // OK | |
drug_dose.setMax('string'); // exception, TypeError | |
drug_dose.setMax(null); // exception, TypeError | |
drug_dose.setMax([]); // exception, TypeError | |
drug_dose.setMax({}); // exception, TypeError | |
drug_dose.setMax(-1); // exception, RangeError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment