コメント |
@DIV
<script type="text/javascript">
// **********************************
// 値を二つ持つオブジェクト
// **********************************
PARAM2 = function () {
this.value1 = null;
this.value2 = null;
};
PARAM2.prototype = {
constructor: PARAM2,
set: function ( a, b ) {
this.value1 = a;
this.value2 = b;
},
plus: function() {
return this.value1 + this.value2;
}
}
p2 = new PARAM2();
// 値をセット
p2.set( 1, 2 );
// 足し算したものを表示
// 3 が表示される
document.write( p2.plus() + "<br />" );
// **********************************
// 値を三つ持つオブジェクト
// **********************************
param3 = function() {
// PARAM2 の定義を param3 の this で呼び出して
// PARAM2 で記述した事を実行する
// 結果的に、PARAM2 内の定義を継承した事になります
PARAM2.call( this );
// param3 での独自のプロパティを追加
this.value3 = null;
};
// しかし、PARAM2 のメソッドは、prototype に定義されているので、
// call では継承されません。
// よって、PARAM2 のメソッドを別に追加します
param3.prototype = Object.create( PARAM2.prototype );
// plus メソッドをオバーライド
param3.prototype.plus = function() {
return this.value1 + this.value2 + this.value3;
}
p3 = new param3();
p3.set( 10, 20 );
p3.value3 = 30;
// 60 が表示される
document.write( p3.plus() + "<br />" );
</script>
@END |