LoginSignup
0
2

More than 5 years have passed since last update.

【GAS】クラスの書き方

Posted at

クラスの書き方

記述方法

test.ga
(function (global) {

  /**
   * コンストラクタ
   * @constructor
   */
  var Test = function () {
  };

  /**
   * メッソド
   * @returns {boolean}
   */
  Test.prototype.main = function () {
    return true;
  };

  global.Test = Test;

})(this);

サンプル

test.ga
function _runTest() {
  // Testクラスのインスタンス化
  var test = new Test();
  // Testクラスのmainメソッドを実行
  test.main();
  // Testクラス内のメンバ変数に設定する
  test.setAaa('あああ');
  // Testクラスン内のメンバー変数を出力する
  test.output();
}

(function (global) {

  /**
   * @constructor
   */
  var Test = function () {
    /**
     * コンストラクタ
     *
     * 「var test = new Test();」のタイミングで実行(インスタンス生成時に実行)
     */
  };

  /**
   * @returns {boolean}
   */
  Test.prototype.main = function () {
    /**
     * メソッド
     *
     * 「test.main();」のタイミングで実行
     */
    Logger.log('mainが実行されました');
    return true;
  };

  /**
   * @returns {boolean}
   */
  Test.prototype.setAaa = function (value) {
    Logger.log('setAAAが実行されました');
    /**
     * メンバー変数設定
     *  引数「value」をメンバー変数に設定する
     */
    this.aaa = value;
    return true;
  };

  /**
   * @returns {boolean}
   */
  Test.prototype.output = function () {
    Logger.log('outputが実行されました');
    /**
     * メンバー変数をログに出力する
     */
    Logger.log(this.aaa);
    return true;
  };

  global.Test = Test;

})(this);
0
2
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
0
2