Flash CS3 文档 |
|||
| ActionScript 2.0 语言参考 > ActionScript 类 > Object > __resolve(Object.__resolve 属性) | |||
public __resolve : Object
对用户定义的函数的引用,该函数在 ActionScript 代码引用未定义的属性或方法时调用。如果 ActionScript 代码引用对象的未定义的属性或方法,则 Flash Player 会确定该对象的 __resolve 属性是否已定义。如果定义了 __resolve,则执行它所引用的函数,并传递未定义的属性或方法的名称。这允许您以程序化方式为未定义属性提供值并为未定义方法提供语句,就好象实际上已经定义了这些属性或方法。此属性对启用高度透明的客户端/服务器通信很有用,并且是调用服务器端方法的推荐方式。
可用性:ActionScript 1.0、Flash Player 6
下面的示例是基于第一个示例以渐进的方式生成的,并演示了 __resolve 属性的五种不同用法。为了帮助理解,不同于上一种用法的关键语句使用粗体。
用法 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 一次。这允许对象方法的“迟缓构造”。迟缓构造是将方法的创建(即“构造”)推迟到第一次使用方法时进行的优化技术。
// 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:下面的示例建立在上一个示例的基础之上,它创建了接受参数的函子。此示例广泛使用参数对象,并且使用了 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
Flash CS3
当前页: http://livedocs.adobe.com/flash/9.0_cn/main/00002045.html