ユーザ定義オブジェクトの書き方は、以下のような感じで良いでしょうか?
動作は問題ないですが、良い書き方とかここはこうしたほうがいいなどの指摘があればお願いします。
<script type="text/javascript">
// Rectangleオブジェクト
// 定数
Rectangle.X = 1;
// コンストラクタ
function Rectangle(w, h){
this.width = w || 0;
this.height = h || 0;
}
// インスタンスメソッド
Rectangle.prototype = {
getArea: function() { return this.width * this.height; },
setArea: function(w, h) { this.width = w || 0; this.height = h || 0; },
toString: function() { return this.width.toString() + ", " + this.height.toString(); }
}
// クラスメソッド
Rectangle.getAreaMax = function() {
var max = 0;
for (var i = 0; i < arguments.length; i++) {
max = Math.max(tmp, arguments[i].getArea());
var tmp = arguments[i].getArea();
}
return max;
}
// テスト
var area1 = new Rectangle(10, 10);
var area2 = new Rectangle(20, 20);
document.write(Rectangle.X + ", ");
document.write(area1.getArea() + ", ");
area1.setArea(5, 5);
document.write(Rectangle.getAreaMax(area1, area2));
</script>
それでよいと思います。
特にコメントはありません。