3

好的,我在自己的 .AS 文件中创建了这两个类 (foo和)。bar这两个类都保存在一个名为custom.

在我称为“Sandbox”的 .FLA 文件中,我将以下代码放入时间线中:

import custom.foo;
import custom.bar;

var f:foo = new foo("FOO");
var b:bar = new bar("BAR");

trace(f.valueOf());
trace(b.valueOf());

f.statement();
b.statement();

我得到以下输出:

食品

酒吧

声明:值为FOO

声明:值为BAR

现在,通常,我不会想太多,但看看类的代码......这是foo.as文件(减去我的评论):

package custom {
        
    public class foo {

        public var property:String;
        public var value:String;

        public function foo (par:String = "?") {
            this.property = par;
            this.value = this.valueOf();
            return;
        }

        prototype.expression = function () {
            trace ("Expression: the value is", this.property);
        }

        public function statement () {
            trace ("Statement: the value is", this.property);
        }

        public function valueOf() {
            return(this.property);
        }

    }
}

...这是bar.as文件(减去我的评论):

package custom {
        
    public class bar {
        public var property:String;
        public var value:String;

        public function bar (par:String = "?") {
            prototype.property = par;
            prototype.value = prototype.valueOf();
            return;
        }

        prototype.expression = function () {
            trace ("Expression: the value is", prototype.property);
        }

        public function statement () {
            trace ("Statement: the value is", prototype.property);
        }

        public function valueOf() {
            return(prototype.property);
        }

    }
}

为什么我使用prototype而不是得到相同的结果thisprototype我发现,虽然这是一个令人烦恼的问题,但除非有人能告诉我它的真正含义,否则无法回答。

我知道this大致翻译为“这个类的这个实例”,但是......是什么prototype意思?

4

3 回答 3

2

ActionScript 3 基于 ECMAScript 规范(正式名称为 ECMA-262),该规范指定了与 JavaScript 相同的标准。

基本上,该规范将prototype对象的属性描述为一种蓝图,该蓝图定义了该对象的新实例在使用new. 您可以将 理解为包含任何字段和方法(或者如果需要)的prototype定义的类对象,这些定义.functionnew

调用方法或访问字段prototype将调用原始函数或将访问“类”对象的原始字段,而不是复制到实例中的方法或字段。因此,您将始终从您的and中获得相同protoype.property但不同的值,因为它们是 和 的实例,分别由.f.propertyb.propertyfoobarnew

回到 ActionScript

以下是一些需要了解prototype的特殊行为。

首先,三个例子说明使用原型和普通成员的区别:

public class Sample {

    public function Sample() {
        trace("this.test():", this.test()); // output: this.test(): member test
        trace("this["test"]():", this["test"]()); // output: this["test"](): member test
    }

    public function test():String {
        return "member test";
    }
}


public class SampleWithPrototype {

    public function SampleWithPrototype() {
        trace("this.test():", this.test()); // COMPILER ERROR: there is no member 'test' defined.
        trace("this["test"]():", this["test"]()); // output: this["test"](): prototype test
    }

    prototype.test = function():String {
        return "prototype test";
    }
}


public class SampleWithBoth {

    public function SampleWithBoth() {
        trace("this.test():", this.test()); // output: this.test(): member test
        trace("this["test"]():", this["test"]()); // output: this["test"](): member test
    }

    public function test():String {
        return "member test";
    }

    prototype.test = function():String {
        return "prototype test";
    }
}

现在解释一下:对象的复制实例属性prototype只能使用下标运算符(this["test"]在示例中)在实例上访问。它们在编译时不会被检查,只能以这种方式访问​​。使用 -notation 访问它.会引发编译时错误 ( SampleWithPrototype)。

在使用prototype和使用常规成员定义机制(如public function ...)同时定义成员时,常规定义的成员会protopype隐藏 -member,因此它变得不可访问(SampleWithBoth)。

此外:您可以prototype.test在运行时使用任何您想要的内容进行覆盖,因此从类创建的每个实例在test被覆盖后都会提供新的行为。而且你不需要用 标记你的班级dynamic

最后:我不推荐使用此功能,因为它会使您的应用程序不可预测且难以调试。prototype除非您将编译器切换到(慢得多)ECMA 标准模式(即非严格模式),否则在 ActionScript 中使用并不常见。然后,您将获得与 JavaScript 相同的行为。

于 2013-03-17T15:27:42.760 回答
0

根据官方文档:

[顶级] Object.prototype

对类或函数对象的原型对象的引用。原型属性会自动创建并附加到您创建的任何类或函数对象。此属性是静态的,因为它特定于您创建的类或函数。例如,如果您创建一个类,则原型属性的值由该类的所有实例共享,并且只能作为类属性访问。您的类的实例不能直接访问原型属性。

类的原型对象是该类的一个特殊实例,它提供了一种在类的所有实例之间共享状态的机制。在运行时,当在类实例上找不到属性时,将检查作为类原型对象的委托以查找该属性。如果原型对象不包含该属性,则该过程继续原型对象的委托在层次结构中连续检查更高级别,直到 Flash 运行时找到该属性。

注意:在 ActionScript 3.0 中,原型继承不是主要的继承机制。类继承驱动类定义中固定属性的继承,是 ActionScript 3.0 中的主要继承机制。

于 2013-03-17T04:47:58.067 回答
0

AS3 和 Javascript 都派生自同一个ECMAScript

在此处输入图像描述

在 javascript 中,原型作为扩展对象和编写面向对象 JS 的唯一方法存在。

它在AS3中也存在相同的含义。但是在AS3中,Adobe编写了类结构,使得原型的使用消失了。因此,在 AS3 中,您可以像使用任何流行的 oop 语言(如 C#/Java)一样编写类并使用它们。

于 2013-03-17T06:48:17.713 回答