JSFiddle - React, Tailwind, and code Playground
JavaScript
var makeCurrencyObject = (function()
{
var sharedMethod1 = function(val)
{//dollar to euro
val = val || (this.toDollars ? this.toDollars() : 1);//<-- carefull with this
return Math.round((val/1.3)*100)/100;
};
var sharedMethod2 = function(val)
{//dollar to pound
val = val || (this.toDollars ? this.toDollars() : 1);//<-- carefull with this
return Math.round((val/1.6)*100)/100;
};
return function(Amount)
{
var returnObject = {
toCents: function()
{
returnObject.currentValue = Amount*100;
return returnObject.currentValue;
},
toDollars: function()
{
returnObject.currentValue = Amount;
return returnObject.currentValue;
},
toEuros: sharedMethod1,
toPounds: sharedMethod2,
currentValue: Amount
};
return returnObject;
};
}());
var c1 = makeCurrencyObject(1.99);
alert('cents = ' + c1.toCents() + '\ncurrent money = ' + c1.currentValue + '\ndollars = ' + c1.toDollars() + '\ncurrent money = ' + c1.currentValue);
var c2 = makeCurrencyObject(2.99);
alert('cents = ' + c2.toCents() + '\ncurrent money = ' + c2.currentValue + '\ndollars = ' + c2.toDollars() + '\ncurrent money = ' + c2.currentValue);
alert('cents = ' + c1.toCents() + '\ncurrent money = ' + c1.currentValue + '\ndollars = ' + c1.toDollars() + '\ncurrent money = ' + c1.currentValue);
alert(c1.toEuros());