Skip to content

Instantly share code, notes, and snippets.

@phi-jp
Created October 23, 2013 07:35
Show Gist options
  • Save phi-jp/7114084 to your computer and use it in GitHub Desktop.
Save phi-jp/7114084 to your computer and use it in GitHub Desktop.
[tmlib.js] tmlib.js におけるクラス定義 ref: http://qiita.com/phi/items/04c4fae5a9a86dc4afc5
tm.define(クラス名, {
superClass: 継承元となるクラス名(省略可)
// 初期化処理
init: function() {
superInit(); // 継承もとの初期化
// TODO: 処理を書いていく
}
});
var hoge = クラス名();
// 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 と表示される
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment