[JS]オブジェクト(literal版)
by tea4two
HTML
<p>1,280円の消費税を含んだ価格は?</p>
<p>
<script type="text/javascript">
var priceGen = {
price: '',
tax: '',
addTax: function() {
this.price += Math.round( this.price * this.tax / 100 );
},
format: function() {
var s = new String( this.price );
var ret = '';
for( var i = s.length - 3; i > 0; i -= 3 ) {
ret = ',' + s.substr(i, 3) + ret;
}
ret = s.substr(0, i + 3) + ret;
return ret; //戻り値
}
};
priceGen.price = 1280;
priceGen.tax = 5;
console.log(priceGen.price);
priceGen.addTax();
document.write( priceGen.format() );
</script>
円 です。
</p>
<p><a href="http://jsfiddle.net/nori2tae/SmBMD/" target="_blank">new Objectを使ったバージョンはこちら</a></p>