Dzwebs.Net

撰写电脑技术杂文十余年

_proto_ 属性和__resolve属性介绍

Admin | 2011-12-31 8:34:53 | 被阅次数 | 5396

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  一、__proto__(Object.__proto__ 属性)

  public __proto__ : Object

  引用用于创建对象的类 (ActionScript 2.0) 或构造函数 (ActionScript 1.0) 的 prototype 属性。在创建对象时,会自动将 __proto__ 属性分配给所有对象。ActionScript 解释程序使用 __proto__ 属性来访问对象的类或构造函数的 prototype 属性,以便弄清楚该对象从其超类中继承了什么属性和方法。

  示例

  以下示例创建一个名为 Shape 的类以及名为 Circle 的 Shape 的子类。

  // Shape class defined in external file named Shape.as
  class Shape {
      function Shape() {}
  }

  // Circle class defined in external file named Circle.as
  class Circle extends Shape{
      function Circle() {}
  }

  Circle 类可用于创建 Circle 的两个实例:

  var oneCircle:Circle = new Circle();
  var twoCircle:Circle = new Circle();

  下面的 trace 语句说明这两个实例的 __proto_ 都引用 Circle 类的 prototype 属性。

  trace(Circle.prototype == oneCircle.__proto__); // Output: true
  trace(Circle.prototype == twoCircle.__proto__); // Output: true

  二、__resolve(Object.__resolve 属性)

  public __resolve : Object

  对用户定义的函数的引用,该函数在 ActionScript 代码引用未定义的属性或方法时调用。如果 ActionScript 代码引用对象的未定义的属性或方法,则 Flash Player 会确定该对象的 __resolve 属性是否已定义。如果定义了 __resolve,则执行它所引用的函数,并传递未定义的属性或方法的名称。这允许您以程序化方式为未定义属性提供值并为未定义方法提供语句,就好象实际上已经定义了这些属性或方法。此属性对启用高度透明的客户端/服务器通信很有用,并且是调用服务器端方法的推荐方式。

  示例

  下面的示例是基于第一个示例以渐进的方式生成的,并说明了 __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


该杂文来自: 动画制作杂文

上一篇:Flash之?: 条件运算符、^ 按位 XOR 运算符、^= 按

下一篇:详细介绍Flash的_accProps属性

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计