tmlib.js では tm.define()
というメソッドを使ってクラス定義することができます.
クラスの定義, 継承はもちろんのこと, 文字列で namespace を含めた名前で
定義することもできます.
Usage
定義
tm.define(クラス名, {
superClass: 継承元となるクラス名(省略可)
// 初期化処理
init: function() {
superInit(); // 継承もとの初期化
// TODO: 処理を書いていく
}
});
使い方
var hoge = クラス名();
Example
Demo
// food 名前空間に Fruit クラスを定義
tm.define("food.Fruit", {
price: 0, // メンバ変数
init: function(price) {
this.price = price;
},
// メンバ関数
getPrice: function() {
return this.price;
}
});
// food 名前空間に Fruit を継承した Apple クラスを定義
tm.define("food.Apple", {
superClass: "food.Fruit",
init: function() {
this.superInit(100); // 親の初期化を呼ぶ
}
});
// メイン処理
tm.main(function() {
// apple クラスを生成(tmlib では new は省略可)
var apple = food.Apple();
// 値段を表示
document.write(apple.getPrice()); // 100 と表示される
});