“原型”这个词在ActionScript中意味着什么?

时间:2013-03-16 19:58:41

标签: actionscript-3 flash actionscript

O.K。,我在他们自己的.AS文件中创建了这两个类(foobar)。这两个类都保存在名为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

     

声明:值为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代替this时,为什么会得到相同的结果? 我发现,虽然这是一个令人烦恼的问题,但除非有人能告诉我prototype实际上意味着什么,否则无法回答。

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

3 个答案:

答案 0 :(得分:1)

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

基本上,规范将对象的prototype属性描述为一种蓝图,用于在使用new创建时定义该对象的新实例的初始属性。您可以将prototype理解为包含任何字段和方法(或function,如果需要)的定义的类对象,这些对象在幕后期间复制到每个实例-new

调用方法或访问prototype上的字段将调用原始函数或将访问“类”对象的原始字段,而不是复制到实例中的方法或字段。因此,您始终会从protoype.property获得相同的值,但与f.propertyb.property不同,因为它们分别是foobar的实例,使用{new创建prototypepublic 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"; } } {1}}。

返回ActionScript

了解prototype的一些特殊行为如下。

首先,三个例子解释使用原型和普通成员的差异:

this["test"]

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

使用protopype定义成员和使用SampleWithBoth等常规成员定义机制的成员时,常规定义的成员会隐藏prototype.test - 成员,因此它变得无法访问({{ 1}})。

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

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

答案 1 :(得分:0)

根据官方文件:

<强> [Top Level] Object.prototype

  

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

     

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

     

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

答案 2 :(得分:0)

AS3&amp; Javascript派生自相同的ECMAScript

enter image description here

在javascript中,原型作为扩展对象的唯一手段而存在。编写面向对象的JS。

它在AS3中也具有相同的含义。但是在AS3中,Adobe编写了类结构,渲染了原型灭绝的使用。所以在AS3你写的课程&amp;像在C#/ Java等任何流行的oop语言中一样使用它们。

相关问题