Bound methods注意

1423 ワード

JAVAと違って、flexの中のBound methods.
Bound methodsは次のように定義されています.
   A bound method, sometimes called a method closure, is simply a method that is extracted from its instance.
Function Closureとの違い:
   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.
bound methodが呼び出されると、そのmethodのthisは、そのメソッドを実装するオブジェクトを参照し、そのメソッドを実装するオブジェクト内の属性を使用することができる.この点とAs 2.0が異なります.
 

class ThisTest
{
private var num:Number = 3;
function foo():void // bound method defined
{
trace("foo's this: " + this);
trace("num: " + num);
}
function bar():Function
{
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 */

このコードのグローバルthisとBound Functionのthisが参照するオブジェクトは異なります.