Flashドキュメンテーション |
|||
| ActionScript 2.0 リファレンスガイド > ActionScript クラス > Object > toString (Object.toString メソッド) | |||
public toString() : String
指定されたオブジェクトをストリングに変換し、返します。
使用できるバージョン : ActionScript 1.0、Flash Player 5
String - ストリング。
この例では、汎用オブジェクトの toString() に対する戻り値を示しています。
var myObject:Object = new Object(); trace(myObject.toString()); // output: [object Object]
このメソッドを上書きし、さらに意味のある値を返すことができます。次の例では、このメソッドがビルトインクラスの Date、Array、および Number に対して上書きされたことを示しています。
// Date.toString() returns the current date and time
var myDate:Date = new Date();
trace(myDate.toString()); // output: [current date and time]
// Array.toString() returns the array contents as a comma-delimited string
var myArray:Array = new Array("one", "two");
trace(myArray.toString()); // output: one,two
// Number.toString() returns the number value as a string
// Because trace() won't tell us whether the value is a string or number
// we will also use typeof() to test whether toString() works.
var myNumber:Number = 5;
trace(typeof (myNumber)); // output: number
trace(myNumber.toString()); // output: 5
trace(typeof (myNumber.toString())); // output: string
次の例では、カスタムクラスの toString() を上書きする方法を示しています。最初に、Vehicle.as という名前のテキストファイルを作成し、そのファイルに Vehicle クラス定義のみを含めて、設定フォルダ内の "Classes" フォルダに格納します。
// contents of Vehicle.as
class Vehicle {
var numDoors:Number;
var color:String;
function Vehicle(param_numDoors:Number, param_color:String) {
this.numDoors = param_numDoors;
this.color = param_color;
}
function toString():String {
var doors:String = "door";
if (this.numDoors > 1) {
doors += "s";
}
return ("A vehicle that is " + this.color + " and has " + this.numDoors + " " + doors);
}
}
// code to place into a FLA file
var myVehicle:Vehicle = new Vehicle(2, "red");
trace(myVehicle.toString());
// output: A vehicle that is red and has 2 doors
// for comparison purposes, this is a call to valueOf()
// there is no primitive value of myVehicle, so the object is returned
// giving the same output as toString().
trace(myVehicle.valueOf());
// output: A vehicle that is red and has 2 doors
このページに新しいコメントが追加された場合に、電子メールでの通知を希望する。 | コメントレポート
現在のページ: http://livedocs.adobe.com/flash/8_jp/main/00002588.html