<These quotes are from the Actionscript 3.0 language reference>
A bound method, sometimes called a method closure, is simply a method that is extracted from its instance. Examples of bound methods include methods that are passed as arguments to a function or returned as values from a function. New in ActionScript 3.0, a bound method is similar to a function closure in that it retains its lexical environment even when extracted from its instance. The key difference, however, between a bound method and a function closure is that the this reference for a bound method remains linked, or bound, to the instance that implements the method. In other words, the this reference in a bound method always points to the original object that implemented the method. For function closures, the this reference is generic, which means that it points to whatever object the function is associated with at the time it is invoked.
Understanding bound methods is important if you use the this keyword. Recall that the this keyword provides a reference to a method’s parent object. Most ActionScript programmers expect that the this keyword always refers to the object or class that contains the definition of a method. Without method binding, however, this would not always be true.
In previous versions of ActionScript, for example, the this reference did not always refer to the instance that implemented the method. When methods are extracted from an instance in ActionScript 2.0, not only is the this reference not bound to the original instance, but also the member variables and methods of the instance’s class are not available. This is not a problem in ActionScript 3.0 because bound methods are automatically created when you pass a method as a parameter. Bound methods ensure that the this keyword always references the object or class in which a method is defined.
The following code defines a class named ThisTest, which contains a method named foo() that defines the bound method, and a method named bar() that returns the bound method. Code external to the class creates an instance of the ThisTest class, calls the bar() method, and stores the return value in a variable named myFunc.
class ThisTest {
private var num:Number = 3;
function foo () { // bound method defined
trace ("foo's this: " + this);
trace ("num: " + num);
}
function bar () {
return foo; // bound method returned
}
}
var myTest:ThisTest = new ThisTest();
var myFunc:Function = myTest.bar();
trace(this); // output: [object global]
myFunc();
/* output:
foo's this: [object ThisTest]
output: num: 3 */
The last two lines of code show that the this reference in the bound method foo() still
points to an instance of ThisTest class, even though the this reference in the line just before
it points to the global object. Moreover, the bound method stored in the myFunc variable still
has access to the member variables of the ThisTest class. If this same code is run in
ActionScript 2.0, the this references would match and the num variable would be undefined.
<These quotes are from the Actionscript 3.0 language reference>
도큐먼트 문서에서 개인적으로 AS3.0 이 새롭게 지원하는 기능중에 가장 좋았던 것 중에 하나다.
더이상 중첩된 메서드 안에서 클래스 안에 선언된 메서드의 참조값을 알아내기 위해 지역변수로 참조를 하지 않아도 된다.
코드 가독성 측면에 있어서 좋지 않았었는데 이젠 끝까지 메서드 선언된 참조 위치를 가지고 있으니 파라미터나 리턴 값에 의해 참조값이 바뀔 염려는 없어진 셈이다.
Recent Comments