Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away