__resolve (Object.__resolve プロパティ)

public __resolve : Object

ActionScript コードが未定義のメソッドまたはプロパティを参照する場合に自動的に呼び出されるユーザー定義関数への参照です。ActionScript コードがオブジェクトの未定義のプロパティまたはメソッドを参照する場合、Flash Player はオブジェクトの __resolve プロパティが定義されているかどうかを判断します。__resolve が定義されていると、参照先の関数が実行され、未定義のプロパティまたはメソッドの名前が渡されます。これにより、プロパティまたはメソッドが実際に定義された場合と同じように、未定義のプロパティの値および未定義のメソッドのステートメントをプログラムによって提供できます。このプロパティは、クライアント/サーバー間で非常に透過的なやり取りが行われるようにする場合に有用であり、サーバーサイドメソッドを呼び出す方法として推奨されています。

対応バージョン : ActionScript 1.0、Flash Player 6

次の例は最初の例から段階的に構築されており、__resolve プロパティの 5 とおりのシンタックスを示しています。わかりやすくするために、前のシンタックスと異なるキーステートメントは太字になっています。

- シンタックス 1 : 次の例では、__resolve を使用し、未定義のプロパティがそれぞれ "Hello, world!" という値を返すオブジェクトを構築しています。

// instantiate a new object
var myObject:Object = new Object();

// define the __resolve function
myObject.__resolve = function (name) {
    return "Hello, world!";
};
trace (myObject.property1); // output: Hello, world!
trace (myObject.property2); // output: Hello, world!

- シンタックス 2 : 次の例では、__resolve を ファンクター (関数を生成する関数) として使用しています。__resolve を使用することで、未定義のメソッドの呼び出しが、myFunction という名前の汎用関数にリダイレクトされます。

// instantiate a new object
var myObject:Object = new Object();

// define a function for __resolve to call
myObject.myFunction = function (name) {
    trace("Method " + name + " was called");
};

// define the __resolve function
myObject.__resolve = function (name) {
     return function () { this.myFunction(name); };
};

// test __resolve using undefined method names
myObject.someMethod(); // output: Method someMethod was called
myObject.someOtherMethod(); //output: Method someOtherMethod was called

- シンタックス 3 : 次の例は、前の例を基にして構築されており、解決されたメソッドをキャッシュに保存する機能を追加しています。メソッドをキャッシュに保存すると、__resolve が各メソッドに対して呼び出されるのは 1 回だけです。したがって、オブジェクトメソッドの遅延構築が可能です。遅延構築は、メソッドが最初に使用されるときまで、メソッドの作成 (構築) を延期する最適化技術です。

// instantiate a new object
var myObject:Object = new Object();
// define a function for __resolve to call
myObject.myFunction = function(name) {
    trace("Method "+name+" was called");
};
// define the __resolve function
myObject.__resolve = function(name) {
    trace("Resolve called for "+name); // to check when __resolve is called
    // Not only call the function, but also save a reference to it
    var f:Function = function () {
        this.myFunction(name);
    };
    // create a new object method and assign it the reference
    this[name] = f;
    // return the reference
    return f;
};
// test __resolve using undefined method names
// __resolve will only be called once for each method name
myObject.someMethod(); // calls __resolve
myObject.someMethod(); // does not call __resolve because it is now defined
myObject.someOtherMethod(); // calls __resolve
myObject.someOtherMethod(); // does not call __resolve, no longer undefined

- シンタックス 4 : 次の例は前の例を基にして構築されており、メソッド名 onStatus() をローカルで使用するために予約し、他の未定義のプロパティと同じ方法で解決されないようにしています。追加されたコードは太字になっています。

// instantiate a new object
var myObject:Object = new Object();
// define a function for __resolve to call
myObject.myFunction = function(name) {
    trace("Method "+name+" was called");
};
// define the __resolve function
myObject.__resolve = function(name) {
    // reserve the name "onStatus" for local use
    if (name == "onStatus") {
        return undefined;
    }
    trace("Resolve called for "+name); // to check when __resolve is called
    // Not only call the function, but also save a reference to it
    var f:Function = function () {
        this.myFunction(name);
    };
    // create a new object method and assign it the reference
    this[name] = f;
    // return the reference
    return f;
};
// test __resolve using the method name "onStatus"
trace(myObject.onStatus("hello"));
// output: undefined

- シンタックス 5 : 次の例は前の例を基にして構築されており、パラメータを受け入れるファンクターを作成しています。この例では、arguments オブジェクトのシンタックスを拡張し、Array クラスのメソッドをいくつか使用しています。

// instantiate a new object
var myObject:Object = new Object();

// define a generic function for __resolve to call
myObject.myFunction = function (name) {
    arguments.shift();
    trace("Method " + name + " was called with arguments: " + arguments.join(','));
};

// define the __resolve function
myObject.__resolve = function (name) {
    // reserve the name "onStatus" for local use
    if (name == "onStatus") {
        return undefined;
    }
    var f:Function = function () { 
        arguments.unshift(name);
        this.myFunction.apply(this, arguments); 
    };
    // create a new object method and assign it the reference
    this[name] = f;
    // return the reference to the function
    return f;
};

// test __resolve using undefined method names with parameters
myObject.someMethod("hello");
// output: Method someMethod was called with arguments: hello

myObject.someOtherMethod("hello","world");
// output: Method someOtherMethod was called with arguments: hello,world

関連項目

arguments, Array


 

このページに新しいコメントが追加された場合に、電子メールでの通知を希望する。 | コメントレポート

現在のページ: http://livedocs.adobe.com/flash/9.0_jp/main/00002045.html