JSFiddle - React, Tailwind, and code Playground
by Aaron Zhang
JavaScript
function forEach(collection, callback) {
for (var i in collection) {
callback(collection[i], i);
}
return collection;
}
function capFirst(s) {
return s[0].toUpperCase() + s.slice(1);
}
function Row(options) {
this.options = options;
var self = this;
forEach(options, function (v, k) {
if (!/Price$/.test(k)) {
if(k == 'gstRate'){
return;
}
if (!options[k + 'Price']) {
throw 'No price defined for ' + k + '!';
}
self['get' + capFirst(k) + 'Cost'] = function () {
var result = +this.options[k] * +this.options[k + 'Price'];
if (this.options.gstRate) {
result /= (1 + this.options.gstRate);
}
return result;
}
}
});
}
var row = new Row({
gu53: 13,
gu53Price: 0.8,
gstRate: 0.1
});
console.log(row.getGu53Cost());