継承まとめ
スーパークラス
var Super = function(num1) { this.num1 = num1; //※1 this.num2 = 20; this.alert_num1 = function() { alert(this.num1); } this.alert_num2 = function() { alert(this.num2); } }
サブクラス
var Sub = function(num1) { this.num1 = num1; this.alert_num1 = function() { alert(this.num1 * 2); } this.alert_num2 = function() { alert(this.num2 * 3); } } Sub.prototype = new Super;
見通しも良くて簡単。
但しコンストラクタ内で引数を用いている箇所(※1)は継承されないので、サブクラス内でも重複して記述しないといけないのが面倒。