类的活动范围已经绑定到类方法-ActionScript3 Tips and Tricks
本文是ActionScript3 Tips and Tricks系列阅读笔记之一Class scope is now bound to class methods,这里是原文地址。
类的活动范围已经绑定到类方法(Class scope is now bound to class methods)
(也不知标题这么翻译是否正确,应该大致是这个意思把,简单的说,这个技巧就是讲解this的引用的问题。ActionScript2中,如果位于某个对象的事件处理函数中,this就指向发出这个事件的对象,而在ActionScript3中,this始终都指向类)
ActionScript3是完全基于类的。当你创建类时,你创建的变量和函数(方法)对这个类以及实例起作用。与ActionScript2不同,方法在被调用的时候保留它所属的类的活动范围,即使这个方法被指派给另一个对象并从另一个对象调用,或者使用Function.call和Function.apply也是如此。范例如下:
(上面一段翻译很可能不准,还是看代码和原文更好)
1package {
2import flash.display.Sprite;
3
4public class ClassScope extends Sprite {
5public function ClassScope() {
6traceThis(); // "Class Instance"
7var obj:Object = new Object();
8obj.traceThis = traceThis;
9obj.traceThis(); // "Class Instance"
10traceThis.call(new Sprite()); // "Class Instance"
11}
12public override function toString():String {
13return "Class Instance";
14}
15public function traceThis():void {
16trace(this);
17}
18}
19}
原文如下:
Class scope is now bound to class methods
ActionScript 3 is entirely class-based. When you create classes, you create variables and functions (methods) which relate to and work with that class and instances of that class. Unlike ActionScript 2, methods in ActionScript 3 now retain their class scope when called, even if assigned to another object and called from that object, or if used with Function.call and Function.apply. Example:
ActionScript Code:(同上)
- 文章ID:311
- 原文作者:zrong
- 原文链接:https://blog.zengrong.net/post/class-scope-is-now-bound-to-class-methods/
- 版权声明:本作品采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可,非商业转载请注明出处(原文作者,原文链接),商业转载请联系作者获得授权。