LoginSignup
1
1

More than 5 years have passed since last update.

[tmlib.js] tmlib.js におけるクラス定義

Last updated at Posted at 2013-10-23

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 と表示される
});

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1