Skip to content

Instantly share code, notes, and snippets.

@maciejlew
Created March 24, 2016 18:03
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 maciejlew/ce9d59ad88ec7cbc9449 to your computer and use it in GitHub Desktop.
Save maciejlew/ce9d59ad88ec7cbc9449 to your computer and use it in GitHub Desktop.
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