コンストラクタ関数による独自のオブジェクトの作成

更新 : 2007 年 11 月

JScript には、コンストラクタ関数を定義して、スクリプト内で使用できるプロトタイプ ベースのカスタム オブジェクトを作成する機能があります。プロトタイプ ベースのオブジェクトのインスタンスを作成するには、最初にコンストラクタ関数を定義する必要があります。これによって、新しいオブジェクトが作成され初期化されます。つまり、プロパティが作成され、初期値が代入されます。初期化が終了すると、作成したオブジェクトへの参照がコンストラクタから返されます。コンストラクタ内部では、作成したオブジェクトは this ステートメントで参照されます。

コンストラクタとプロパティ

次の例では、pasta オブジェクトのコンストラクタ関数を定義しています。コンストラクタは、this ステートメントを使ってオブジェクトを初期化します。

// pasta is a constructor that takes four parameters.
function pasta(grain, width, shape, hasEgg) {
   this.grain = grain;    // What grain is it made of?
   this.width = width;    // How many centimeters wide is it?
   this.shape = shape;    // What is the cross-section?
   this.hasEgg = hasEgg;  // Does it have egg yolk as a binder?
}

オブジェクトのコンストラクタを定義したら、new 演算子を使用してそのオブジェクトのインスタンスを作成できます。次の例では、pasta コンストラクタを使用して spaghetti オブジェクトと linguine オブジェクトを作成します。

var spaghetti = new pasta("wheat", 0.2, "circle", true);
var linguine = new pasta("wheat", 0.3, "oval", true);

オブジェクトのインスタンスにプロパティを動的に追加できますが、変更はそのインスタンスにしか影響を与えません。

// Additional properties for spaghetti. The properties are not added
// to any other pasta objects.
spaghetti.color = "pale straw";
spaghetti.drycook = 7;
spaghetti.freshcook = 0.5;

コンストラクタ関数を変更せずに、オブジェクトのすべてのインスタンスにプロパティを追加する場合は、コンストラクタのプロトタイプ オブジェクトにプロパティを追加します。詳細については、「高度なオブジェクト作成 (JScript)」を参照してください。

// Additional property for all pasta objects. 
pasta.prototype.foodgroup = "carbohydrates";

コンストラクタとメソッド

オブジェクトの定義にメソッド (関数) を含めることができます。オブジェクトの定義にメソッドを含める方法の 1 つは、他の場所で定義された関数を参照するプロパティをコンストラクタ関数に追加することです。これらの関数は、コンストラクタ関数と同様に、現在のオブジェクトを this ステートメントで参照します。

前の例で定義した pasta コンストラクタ関数を拡張して、関数がオブジェクトの値を表示するときに呼び出される toString メソッドを追加します。通常は、文字列が必要な状況でオブジェクトを使用すると、JScript がオブジェクトの toString メソッドを使用します。toString メソッドを明示的に呼び出す必要はほとんどありません。

// pasta is a constructor that takes four parameters.
// The properties are the same as above.
function pasta(grain, width, shape, hasEgg) {
   this.grain = grain;    // What grain is it made of?
   this.width = width;    // How many centimeters wide is it?
   this.shape = shape;    // What is the cross-section?
   this.hasEgg = hasEgg;  // Does it have egg yolk as a binder?
   // Add the toString method (defined below).
   // Note that the function name is not followed with parentheses;
   // this is a reference to the function itself, not a function call.
   this.toString = pastaToString;
}

// The function to display the contents of a pasta object.
function pastaToString() {
   return "Grain: " + this.grain + "\n" +
          "Width: " + this.width + " cm\n" +
          "Shape: " + this.shape + "\n" +
          "Egg?:  " + Boolean(this.hasEgg);
}

var spaghetti = new pasta("wheat", 0.2, "circle", true);
// Call the method explicitly.
print(spaghetti.toString());
// The print statement takes a string as input, so it
//  uses the toString() method to display the properties
// of the spaghetti object.
print(spaghetti);

出力は次のようになります。

Grain: wheat
Width: 0.2 cm
Shape: circle
Egg?:  true
Grain: wheat
Width: 0.2 cm
Shape: circle
Egg?:  true

参照

その他の技術情報

プロトタイプ ベースのオブジェクト

JScript オブジェクト