Pot.js と PotLite.js で利用可能。
新しいコンストラクタを作成します。
prototype と共にコンストラクタを生成して返します。
任意に引数 name が指定されると、toString に使われます。
引数 init は初期化する関数名または関数を任意で指定します。
引数 init が省略されて、proto 内に 'init' というメソッドがあると、それを初期化する関数と扱います。
Pot.globalize() が適応されている場合、Pot.createConstructor() が createConstructor() で実行できます。
新しいコンストラクタが返ります。
// proto 内に init で初期化メソッドを指定
var Hoge = Pot.createConstructor('Hoge', {
init : function(a, b, c) {
this.value = a + b + c;
},
getHoge : function() {
return 'hogehoge';
}
});
Pot.debug(new Hoge(1, 2, 3).value); // 6
Pot.debug(new Hoge().getHoge()); // 'hogehoge'
// 初期化関数を関数で指定
var Fuga = Pot.createConstructor({
value : 1,
addValue : function(v) {
this.value += v;
return this;
},
getValue : function() {
return this.value;
}
}, function(a, b, c) {
this.value += a + b + c;
});
Pot.debug(new Fuga(1, 2, 3).value); // 7
Pot.debug(new Fuga(1, 2, 3).addValue(10).getValue()); // 17
// initialize という名前で初期化関数を指定
var Piyo = Pot.createConstructor('Piyo', {
initialize : function(a, b, c) {
this.value = a + b + c;
},
getValue : function() {
return this.value;
}
}, 'initialize');
Pot.debug(new Piyo(10, 20, 30).getValue()); // 60