Actionscript 3 这个词是什么意思;原型;在动作脚本中是什么意思?
好的,我创建了这两个类(Actionscript 3 这个词是什么意思;原型;在动作脚本中是什么意思?,actionscript-3,flash,actionscript,Actionscript 3,Flash,Actionscript,好的,我创建了这两个类(foo和bar),每个类都有自己的.AS文件。这两个类都保存在名为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
foo
和bar
),每个类都有自己的.AS文件。这两个类都保存在名为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
文件(减去我的注释):
…这是栏。作为文件(减去我的注释):
为什么我在使用prototype
而不是this
时得到了相同的结果?
我发现,尽管这是一个令人烦恼的问题,但除非有人能告诉我原型的实际含义,否则无法回答这个问题
我知道这个
大致翻译成“这个类的这个实例”,但是原型是什么意思?根据官方文件:
对类或函数对象的原型对象的引用。这个
prototype属性将自动创建并附加到任何类
或创建的函数对象。此属性是静态的,因为它
特定于您创建的类或函数。例如,如果
创建一个类时,原型属性的值由
类的所有实例,并且只能作为类属性访问。
类的实例无法直接访问prototype属性
类的原型对象是该类的一个特殊实例
提供一种机制,用于跨所有实例共享状态
班级。在运行时,如果在类实例上找不到属性,
委托,即类原型对象,被检查是否存在该问题
财产。如果原型对象不包含该属性,则
该过程将继续进行原型对象的委托签入
层次结构中的连续更高级别,直到Flash运行时
查找属性
注意:在ActionScript 3.0中,原型继承不是主要继承
遗传机制。类继承,它驱动
继承类定义中的固定属性是主要的
ActionScript 3.0中的继承机制
AS3和Javascript都源自同一个版本
在javascript中,原型作为扩展对象的唯一手段存在&编写面向对象的JS
它在AS3中也有相同的含义,但是在AS3中,Adobe编写了类结构,使得原型的使用消失了。因此,在AS3中编写类并使用它们,就像在C#/Java等流行的oop语言中一样。ActionScript 3基于ECMAScript规范(正式称为ECMA-262),该规范指定了与JavaScript相同的标准
基本上,规范将对象的prototype
属性描述为一种蓝图,定义了使用new
创建该对象的新实例的初始属性。您可以将原型
理解为类对象,该类对象包含在新建
的幕后过程中复制到每个实例的任何字段和方法(或函数
,如果需要)的定义
在原型上调用方法或访问字段将调用原始函数或访问“class”对象的原始字段,而不是复制到实例中的方法或字段。因此,您将始终从prototype.property
获得相同的值,但与f.property
和b.property
不同,因为它们分别是foo
和bar
的实例,使用new
创建
回到动作脚本
关于原型
需要了解的一些特殊行为如下
首先,用三个例子来解释使用原型和普通成员的区别:
现在的解释是:prototype
对象复制的实例属性只能使用下标操作符在实例上访问(this[“test”]
)。它们在编译时不会被检查,只能通过这种方式访问。使用
-符号访问它会引发编译时错误(SampleWithPrototype
)
当使用prototype
定义成员和使用常规成员定义机制(如public function…
)定义成员时,常规定义的成员会隐藏protopype
-成员,因此它变得不可访问(samplewithtwith
)
此外:您可以在运行时用任何需要的内容覆盖prototype.test
,因此在覆盖test
后从类创建的每个实例都提供了新的行为。而且您不需要使用动态标记类
最后:我不建议使用此功能,因为它会使您的应用程序不可预测且难以调试。除非将编译器切换到(速度慢得多)ECMA标准模式(即非严格模式),否则在ActionScript中使用原型并不常见。然后,您将获得与JavaScript中相同的行为。谢谢,这非常有帮助!虽然我不明白你说的一些话,但你很清楚地回答了我的问题。不客气。如果你能说出你不懂的话,那么我可以改进我的答案。我想我应该说我读到了。。。这对我没有任何帮助,因为我在这方面有点慢。对不起,这是我不清楚的过错。
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);
}
}
}
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);
}
}
}
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";
}
}